Unzip a zipfile in the same hierachy using java.util.ZipFile?

Zip doesn't offer directory structure per se. The tree alike structure is built by having full path of each entry. ZipFile enumerates the entries in the same way they have been added to the file.

Zip doesn't offer directory structure per se. The tree alike structure is built by having full path of each entry. ZipFile enumerates the entries in the same way they have been added to the file.

Note: java.util.ZipEntry.isDirectory() just tests if the last character of the name is '/', that's how it works. What you need to extract the files into the same directory. Parse then name like that: for(ZipEntry zipEntry : java.util.Collections.

List(zipFile.entries())){//lazislav String name = zipEntry.getName(); int idx = name. LastIndexOf('/'); if (idx>=0) name=name. Substring(idx) if (name.length()==0) continue; File f = new File(targetDir, name); } That shall do it more or less (you still need to take care of duplicate file names, etc).

This is mine. In file you specify the file you want to expand in target dir you have to specify the target location as "new File("/tmp/foo/bar")". If you want to extract in the current directory you can specify targetDir = new File(".") public static void unzip(File file, File targetDir) throws ZipException, IOException { targetDir.mkdirs(); ZipFile zipFile = new ZipFile(file); try { Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File targetFile = new File(targetDir, entry.getName()); if (entry.isDirectory()) { targetFile.mkdirs(); } else { InputStream input = zipFile.

GetInputStream(entry); try { OutputStream output = new FileOutputStream(targetFile); try { copy(input, output); } finally { output.close(); } } finally { input.close(); } } } } finally { zipFile.close(); } } private static void copy(InputStream input, OutputStream output) throws IOException { byte buffer = new byte4096; int size; while ((size = input. Read(buffer))! = -1) output.

Write(buffer, 0, size); } Worked for me. Good luck.

Great code worked well for me as well kept all of the folders and the structure inside the zip the same just as I needed it – GFlam Jun 3 at 20:13 actually after using it a bit doesn't work on larger files for some reason I have a zip that is 105mbs and it doesn't even start to unzip it – GFlam Jun 3 at 21:19 funny. Do you get any exception? – Luigi R.

Viggiano Jul 12 at 14:44.

If the ZipFile entry has a name /a/b/c/file. Txt, then you can work out the directory name /a/because and then create a directory in your tree called a/b/c.

ZipFile zipFile = new ZipFile("archive. Zip"); try { for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { new File(entry.getName()).mkdirs(); } else { InputStream in = zipFile. GetInputStream(entry); try { OutputStream out = new BufferedOutputStream(new FileOutputStream(entry.getName())); try { // this util class is taken from apache commons io (see http://commons.apache.org/io/) IOUtils.

Copy(in, out); } finally { out.close(); } } finally { in.close(); } } } } catch (IOException e) { e.printStackTrace(); } finally { zipFile.close(); }.

Here's the one I use all the times. It should directly work after a copy/paste and in any circumstances. Public static File unzip(File inFile, File outFolder) { final int BUFFER = 2048; try { BufferedOutputStream out = null; ZipInputStream in = new ZipInputStream( new BufferedInputStream( new FileInputStream(inFile))); ZipEntry entry; while((entry = in.getNextEntry())!

= null) { //System.out. Println("Extracting: " + entry); int count; byte data = new byteBUFFER; //We will try to reconstruct the entry directories File entrySupposedPath = new File(outFolder.getAbsolutePath()+File. Separator+entry.getName()); //Does the parent folder exist?

If (!entrySupposedPath.getParentFile().exists()){ entrySupposedPath.getParentFile().mkdirs(); } // write the files to the disk out = new BufferedOutputStream( new FileOutputStream(outFolder.getPath() + "/" + entry.getName()),BUFFER); while ((count = in. Read(data,0,BUFFER))! = -1) { out.

Write(data,0,count); } out.flush(); out.close(); } in.close(); return outFolder; } catch(Exception e) { e.printStackTrace(); return inFile; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions