Unpacking multi-user ZIP files using Java

I need to unzip a set of files that are a zip archive. This is not a set of zip files, it is one large zip file that has been split into several zip files based on size requirements.

For example, if you have a zip file 2.5MB, and your mail system only supports 1MB files, you can ask Zip to create 3 files no more than 1 MB.

So, he creates a.zip.001, a.zip.002, a.zip.003 ... different libraries call them differently, but essentially they all work the same way.

How do you unzip this in java? This is not like the compression of libs in std supports this.

Thanks.

+4
source share
1 answer

Try combining all the files into one file and then extracting one file. Sort of:

File dir = new File("D:/arc"); FileOutputStream fos = new FileOutputStream(new File( "d:/arc/archieve-full.zip")); FileInputStream fis = null; Set<String> files = new TreeSet<String>(); for (String fname : dir.list()) { files.add(fname); } for (String fname : files) { try { fis = new FileInputStream(new File(dir.getAbsolutePath(), fname)); byte[] b = new byte[fis.available()]; fis.read(b); fos.write(b); } finally { if (fis != null) { fis.close(); } fos.flush(); } } fos.close(); ZipFile zipFile = new ZipFile("d:/arc/archieve-full.zip"); /*extract files from zip*/ 

Update: TreeSet used to sort file names, since dir.list() does not guarantee alphabetical order.

+2
source

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


All Articles