Iterate over all directories in java zip file

I am currently developing a tool that will allow me to modify the md5 zip file. The directory structure of the file looks like

baselines-> models -> icons -> lang -> (a bunch of files here) 

However, when I run my code, none of these directories iterate. The result gives me:

 Name:model/visualization_dependency.xml Name:model/visualization_template.xml Name:model/weldmgmt_dependency.xml Name:model/weldmgmt_template.xml 

I was expecting something like model / baseline / somefile.xml to appear on the output, but that is not the case. Any suggestions?

 byte[] digest = null; MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); ZipEntry current; while((current = entry.getNextEntry()) != null){ //ZipEntry current = entry.getNextEntry(); System.out.println("Size:" + current.getSize()); System.out.println("Name:" + current.getName()); if(current.isDirectory()){ digest = this.encodeUTF8(current.getName()); md5.update(digest); } else{ int size = (int)current.getSize(); digest = new byte[size]; entry.read(digest, 0, size); md5.update(digest); } } digest = md5.digest(); entry.close(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+6
source share
2 answers

Once you verify that the existing folder is a directory, you need to iteratively browse through all the files in the directory and process each one.

Example:

 if(current.isDirectory()){ System.out.println("Directory: " + file.getName()); //Get list of files by file.listFiles() and pass it to // to other method that will do processing. digest = this.encodeUTF8(current.getName()); md5.update(digest); } 

To issue this question, it details the process. Directory Iteration in Java

+3
source

I think your code is perfect. I suspect your zip file does not contain directories. They do not need!

For example, a zip file created using "a / b / c / d.txt" is created here. When I originally created it, directories were added to the zip file:

 $ unzip -l a.zip Archive: a.zip Length Date Time Name --------- ---------- ----- ---- 0 2012-06-12 14:22 a/ 0 2012-06-12 14:22 a/b/ 0 2012-06-12 14:22 a/b/c/ 19 2012-06-12 14:22 a/b/c/d.txt --------- ------- 19 4 files 

But then I deleted the directories from the zip index:

 $ zip -d a.zip a/b/c deleting: a/b/c/ $ zip -d a.zip a/b deleting: a/b/ $ zip -d a.zip a deleting: a/ 

And now, when I have listed its contents, I'm pretty sure that only the file will appear. No catalogs:

 $ unzip -l a.zip Archive: a.zip Length Date Time Name --------- ---------- ----- ---- 19 2012-06-12 14:22 a/b/c/d.txt --------- ------- 19 1 file 

Note: when I unzipped the same file, it created the a / b / c / directory before extracting the d.txt file, even though the zip index itself did not contain directories. Thus, it seems that entries in zip files are completely optional.

+2
source

Source: https://habr.com/ru/post/917915/


All Articles