• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to open zip properly and check it's files content

 
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have byte array which was converted from zip file. I would like to check in the test whether its files contains proper content. So for example zip consists of 2 text files. First file contains "HEY" word, and the sceond "HEY2". I would like to after reconverting whether everything is all right.

I have made something like this:



and it prints:

Entry: 1.txt len 5 added 03/16/21
Entry: 2.txt len 6 added 03/16/21

how can I go deeper inside every file?

 
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ZipInputStream (and for that matter ZipOutputStream as well) are a bit odd, in that they reset themselves for each entry. That means that once you've used getNextEntry, you can start reading from the ZipInputStream and it will only read the contents of that entry. Its read methods will return -1 once the entry is read. When you call getNextEntry again, you can start reading from the ZipInputStream again, etc.

So in short, to read everything from every entry:

One warning: don't close the ZipInputStream, because it will close the entire ZipInputStream. You won't be able to read any more entries.

That last part can be annoying with ZipOutputStream when you need to wrap it into some OutputStream that needs to be closed to finalize its work (e.g. ObjectOutputStream). You can use CloseShieldOutputStream (again from Apache Commons IO) to wrap the ZipOutputStream, then wrap that. Closing the outer OutputStream will then stop at the CloseShieldOutputStream:
reply
    Bookmark Topic Watch Topic
  • New Topic