Creating Folders in a Zip Folder in Java [duplicate] - java

This question already has answers here:
directories in a zip file when using java.util.zip.ZipOutputStream
(6 answers)
Closed 9 years ago.
My task requires me to save a file directory into a zip folder. My only problem is I need to keep the sub-folders as folders from the main Directory. The file system will look something like
C\\Friends
C:\\Friends\\Person1\\Information.txt
C:\\Friends\\Person2\\Information.txt
C:\\Friends\\Person3\\Information.txt
.
.
.
Right now I am able to write just the txt files inside of my zip folder, but in my zip folder I need to keep that folder structure. I know the way my code is right now will tell me the file I'm trying to write is closed(No access). My Functions thus far:
private String userDirectroy = "" //This is set earlier in the program
public void exportFriends(String pathToFile)
{
String source = pathToFile + ".zip";
try
{
String sourceDir = userDirectory;
String zipFile = source;
try
{
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, fileSource);
zout.close();
System.out.println("Zip file has been created!");
}
catch(Exception e)
{
}
}
catch(Exception e)
{
System.err.println("First Function: " + e);
}
}
private static void addDirectory(ZipOutputStream zout, File fileSource) {
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for(int i=0; i < files.length; i++)
{
if(files[i].isDirectory())
{
try
{
byte[] buffer = new byte[1024];
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
}
catch(Exception e)
{
System.err.println(e);
}
addDirectory(zout, files[i]);
continue;
}
try
{
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while((length = fin.read(buffer)) > 0)
{
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
catch(IOException ioe)
{
System.out.println("IOException :" + ioe);
}
}
}
Any help would be much appreciated. Thank You

For each folder, you need to add a empty ZipEntry of the path.
For each file, you need to supply both the path and file name. This will require you to know the part of the path to strip off, this would be everything after the start directory
Expanded concept
So, from your example, if the start directory is C:\Friends, then the entry for C:\Friends\Person1\Information.txt should look like Person1\Information.txt
public void exportFriends(String pathToFile) {
String source = pathToFile + ".zip";
try {
String sourceDir = "C:/Friends";
String zipFile = source;
try {
FileOutputStream fout = new FileOutputStream(zipFile);
ZipOutputStream zout = new ZipOutputStream(fout);
File fileSource = new File(sourceDir);
addDirectory(zout, sourceDir, fileSource);
zout.close();
System.out.println("Zip file has been created!");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getRelativePath(String sourceDir, File file) {
// Trim off the start of source dir path...
String path = file.getPath().substring(sourceDir.length());
if (path.startsWith(File.pathSeparator)) {
path = path.substring(1);
}
return path;
}
private static void addDirectory(ZipOutputStream zout, String sourceDir, File fileSource) throws IOException {
if (fileSource.isDirectory()) {
// Add the directory to the zip entry...
String path = getRelativePath(sourceDir, fileSource);
if (path.trim().length() > 0) {
ZipEntry ze = new ZipEntry(getRelativePath(sourceDir, fileSource));
zout.putNextEntry(ze);
zout.closeEntry();
}
File[] files = fileSource.listFiles();
System.out.println("Adding directory " + fileSource.getName());
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
addDirectory(zout, sourceDir, files[i]);
} else {
System.out.println("Adding file " + files[i].getName());
//create byte buffer
byte[] buffer = new byte[1024];
//create object of FileInputStream
FileInputStream fin = new FileInputStream(files[i]);
zout.putNextEntry(new ZipEntry(getRelativePath(sourceDir, files[i])));
int length;
while ((length = fin.read(buffer)) > 0) {
zout.write(buffer, 0, length);
}
zout.closeEntry();
//close the InputStream
fin.close();
}
}
}
}

Related

How to release a file in Java GUI without closing

I've created a pretty simple Java GUI to browse/load a zip file on Windows platform to begin unzipping and then do some file checking.
Everything works fine except that I have to close the GUI window in order to delete the zip file that has been opened in the GUI.In my finally block of the unzipping method, I've tried adding the following:
public static String unZip(String path)
{
int count = -1;
String savepath = "";
File file = null;
InputStream is = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
savepath = path.substring(0, path.lastIndexOf("\\")) + File.separator; //File saving directory
new File(savepath).mkdir(); //create the saving directory
ZipFile zipFile = null;
String topLevelDirName="";
try
{
zipFile = new ZipFile(path,Charset.forName("gbk")); //Encoding
Enumeration<?> entries = zipFile.entries();
int levelCount=0;
while(entries.hasMoreElements())
{
byte buf[] = new byte[buffer];
ZipEntry entry = (ZipEntry)entries.nextElement();
String filename = entry.getName();
boolean ismkdir = false;
if(filename.lastIndexOf("/") != -1){ //To check if there is a directory
ismkdir = true;
}
filename = savepath + filename;
if(entry.isDirectory()){ //If it is a directory
levelCount++;
file = new File(filename);
file.mkdirs();
if(levelCount==1)
topLevelDirName = filename;
continue;
}
file = new File(filename);
if(!file.exists()){
if(ismkdir){
new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs();
}
}
file.createNewFile(); //Create the file
is = zipFile.getInputStream(entry);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, buffer);
while((count = is.read(buf)) > -1)
{
bos.write(buf, 0, count);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
try{
if(bos != null){
bos.close();
}
if(fos != null) {
fos.close();
}
if(is != null){
is.close();
}
if(zipFile != null){
zipFile.close();
}
}catch(Exception e) {
e.printStackTrace();
}
return topLevelDirName;
}
}
However, I am still not able to delete the zip unless explicitly close the GUI.
Wonder if there is anything to do with the Windows file handle?Thanks in advance.
Java 8 introduced the try-with-resources Statement to make this kind of situation simpler and cleaner.
One of the issues you have is, if any one of the attempts to close the many resources you have open fails, then none of the others will be closed
public static String unZip(String path) throws IOException {
int count = -1;
File sourceFile = new File(path);
String name = sourceFile.getName();
name = name.substring(0, name.lastIndexOf(".zip"));
File sourcePath = new File(sourceFile.getParent(), name);
System.out.println("SavePath = " + sourcePath);
if (!sourcePath.exists() && !sourcePath.mkdirs()) {
throw new IOException("Could not create directory " + sourcePath);
}
String topLevelDirName = "";
try (ZipFile zipFile = new ZipFile(sourceFile)) {
Enumeration<?> entries = zipFile.entries();
int levelCount = 0;
byte buf[] = new byte[1024];
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String filename = entry.getName();
File file = new File(sourcePath, filename);
if (entry.isDirectory()) { //If it is a directory
levelCount++;
System.out.println("Make directory " + file);
if (!file.exists() && !file.mkdirs()) {
throw new IOException("Could not create directory " + filename);
}
} else {
System.out.println("Extract to " + file);
try (InputStream is = zipFile.getInputStream(entry);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
while ((count = is.read(buf)) > -1) {
bos.write(buf, 0, count);
}
}
}
}
}
return topLevelDirName;
}
I've update the code slightly to try and make it a little cleaner and simpler and to take advantage of the available APIs

How to copy content from a zip to an Android directory?

Hello everyone.
Ask this question so that they could give me a hand and guide me on my way.
My problem
I want to be able to unzip a zip into a folder or directory on the SD card, but my code has not reached the target. Its error is that it does not decompress or copy any of the files in the destination directory. The zip is located in the resources resource folder.
My code
private boolean copyFile1(String filename1, String outPath1) {
AssetManager assetManager = this.getAssets();
final int CHUNK_SIZE = 1024 * 4;
InputStream in;
OutputStream out;
try {
in = assetManager.open(filename1);
String newFileName = outPath1;
ZipInputStream zipStream = new ZipInputStream(in);
ZipEntry zEntry = null;
while ((zEntry = zipStream.getNextEntry()) != null) {
if (zEntry.isDirectory()) {
} else {
FileOutputStream fout = new FileOutputStream(new File(outPath1));
BufferedOutputStream bufout = new BufferedOutputStream(fout);
byte[] buffer = new byte[CHUNK_SIZE];
int read = 0;
while ((read = zipStream.read(buffer)) != -1) {
bufout.write(buffer, 0, read);
}
zipStream.closeEntry();
bufout.close();
fout.close();
}
}
zipStream.close();
Log.d("Unzip", "Unzipping complete. path : " );
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
return true;
}
If they realize where I failed in my code or I know otherwise. Please let me know. Thank you

export/zip eclipse project via java program for Windows and MAC

I need to export a (java) eclipse project without using eclipse. I tried to copy all relevant files and pack them with a ZipOutputStream. So good so far, I can load this zip-file in Windows. But MAC users have the problem, that the file structure is not automaticaly detected and create files with names like "vorlage\src\de\tuberlin..." next to the src directory.
(example picture)
Is there a way to properly export an eclipse project with a java programm?
The reason is that I created programming exercises for a lecture and marked the solutions with special comments. During copying the files these comments and the solutions are left out.
One solution would be importing the zip-file in Windos and exporting again with the help of eclipse. But this costs a lot of time doing this for each exercise.
Although I cannot see any difference between the two zip-files, there seems to be something.
Here my code to copy the files:
public static void copyDir(File source, File target, ArrayList<String> exclude)
throws FileNotFoundException, IOException {
File[] files = source.listFiles();
File newFile = null;
target.mkdirs();
if (files != null) {
for (int i = 0; i < files.length; i++) {
newFile = new File(target.getAbsolutePath() + System.getProperty("file.separator") + files[i].getName());
if (files[i].isDirectory()) {
copyDir(files[i], newFile, exclude);
}
else if ( !exclude.contains(files[i].getName()) ) {
copyFile(files[i], newFile);
}
}
}
}
public static void copyFile(File file, File target) throws FileNotFoundException, IOException {
if (file.getName().endsWith(".java")) {
copyJavaFile(file, target);
return;
}
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target, true));
int bytes = 0;
while ((bytes = in.read()) != -1) {
out.write(bytes);
}
in.close();
out.close();
}
private static void copyJavaFile(File file, File target) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(target);
boolean stopped = false;
String line = null;
while ( (line = in.readLine() ) != null) {
stopped |= line.contains("IF EXCLUDE");
if (!stopped && !line.contains("ENDIF"))
out.println(line);
stopped &= !line.contains("ELSE");
}
in.close();
out.close();
}
I copy the following (sub-)directories/files:
.settings
src
.classpath
.project
I create the zip-file with:
public Zip(final File projDir, final File outFile) {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(
new BufferedOutputStream(
new FileOutputStream(outFile)));
for (File file : projDir.listFiles()) {
writeFiles(file, output, "");
}
output.finish();
} catch (IOException ex) {
System.err.println("IO Error: " + ex.getMessage());
} finally {
try {output.close();} catch (IOException ex) {}
}
}
private void writeFiles(File f, ZipOutputStream output, String dir) throws IOException {
if (f.isDirectory()) {
// recursively write files in directory
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + File.separator);
}
} else {
// write this file to archive
FileInputStream fis = new FileInputStream(f);
ZipEntry entry = new ZipEntry(dir + f.getName());
entry.setTime(f.lastModified());
output.putNextEntry(entry);
copy(fis, output);
output.closeEntry();
fis.close();
}
}
private void copy(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = is.read(buffer)) > 0) {
os.write(buffer, 0, bytes);
}
}
It is a problem of the path separator. Using "/" instead of "File.separator" solves the problem.
In the example above the for-loop in the method writeFiles(...) must be:
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + "/");
}
See also this answer.

How to move files and directories

How to move files and directories into another directory in Java? I am using this technique to copy but I need to move:
File srcFile = new File(file.getCanonicalPath());
String destinationpt = "/home/dev702/Desktop/svn-tempfiles";
copyFiles(srcFile, new File(destinationpt+File.separator+srcFile.getName()));
You can try this:
srcFile.renameTo(new File("C:\\folderB\\" + srcFile.getName()));
Have you read this "http://java.about.com/od/InputOutput/a/Deleting-Copying-And-Moving-Files.htm
"
Files.move(original, destination, StandardCopyOption.REPLACE_EXISTING)
move the files to destination.
If you want to move directory
Use this
File dir = new File("FILEPATH");
if(dir.isDirectory()) {
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++) {
//move files[i]
}
}
Java.io.File does not contains any ready make move file method, but you can workaround with the following two alternatives :
File.renameTo()
Copy to new file and delete the original file.
public class MoveFileExample
{
public static void main(String[] args)
{
try{
File afile =new File("C:\\folderA\\Afile.txt");
if(afile.renameTo(new File("C:\\folderB\\" + afile.getName()))){
System.out.println("File is moved successful!");
}else{
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
For Copy and delete
public class MoveFileExample
{
public static void main(String[] args)
{
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile =new File("C:\\folderA\\Afile.txt");
File bfile =new File("C:\\folderB\\Afile.txt");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
//delete the original file
afile.delete();
System.out.println("File is copied successful!");
}catch(IOException e){
e.printStackTrace();
}
}
}
HOPE IT HELPS :)

Java zip util adding extra folders

I'm having an issue zipping up folders.
I.e. I'll have a folder like this
C:\Lazy\Test
containing files File1.cpp, File2.hpp,.. etc
The zipped up folder looks like C:\Lazy\Test.zip -> Lazy\Test which contains all the cpp and hpp files.
I want to remove the extra subfolders (Lazy\Test) that get created. Why is this happening?
In other words, the zipped up files are not directly underneath the zip file, I have to navigate two more folders to get to them.
Where can I find this issue in the code?
private void zipDirectory() {
File lazyDirectory = new File(defaultSaveLocation);
File[] files = lazyDirectory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.println("Zipping up " + file);
zipContents(file);
}
}
}
public static void addToZip(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
public static void zipContents(File dirToZip) {
List<File> fileList = new ArrayList<File>();
File[] filesToZip = dirToZip.listFiles();
for (File zipThis : filesToZip) {
String ext = "";
int i = zipThis.toString().lastIndexOf('.');
if (i > 0) {
ext = zipThis.toString().substring(i+1);
}
if(ext.matches("cpp|bem|gz|h|hpp|pl|pln|ppcout|vec|xml|csv")){
fileList.add(zipThis);
}
}
try {
FileOutputStream fos = new FileOutputStream(dirToZip.getName() + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
for (File file : fileList) {
addToZip(file.toString(), zos);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Change the addToZip file to take File object. Use it to open the file stream, but only use File#getName to create the ZipEntry, as follows...
public static void addToZip(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
System.out.println("Writing '" + fileName + "' to zip file");
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}

Categories

Resources