How do you expand a split zip volume in Java?

I need to assemble a 100-part assembly file and extract the contents. I tried just combining the zip volumes in the input stream, but this does not work. Any suggestions would be appreciated.

Thanks.

+2
source share
2 answers

Here is the code you can start with. It extracts one file entry from a multi-volume mail archive:

package org.test.zip; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.SequenceInputStream; import java.util.Arrays; import java.util.Collections; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { public static void main(String[] args) throws IOException { ZipInputStream is = new ZipInputStream(new SequenceInputStream(Collections.enumeration( Arrays.asList(new FileInputStream("test.zip.001"), new FileInputStream("test.zip.002"), new FileInputStream("test.zip.003"))))); try { for(ZipEntry entry = null; (entry = is.getNextEntry()) != null; ) { OutputStream os = new BufferedOutputStream(new FileOutputStream(entry.getName())); try { final int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; for(int readBytes = -1; (readBytes = is.read(buffer, 0, bufferSize)) > -1; ) { os.write(buffer, 0, readBytes); } os.flush(); } finally { os.close(); } } } finally { is.close(); } } } 
+5
source

Just notice to make it more dynamic - 100% based on the mijer code below.

  private void CombineFiles (String[] files) throws FileNotFoundException, IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(files.length); for (int x = 0; x < files.length; x++) v.add(new FileInputStream(inputDirectory + files[x])); Enumeration<FileInputStream> e = v.elements(); SequenceInputStream sequenceInputStream = new SequenceInputStream(e); ZipInputStream is = new ZipInputStream(sequenceInputStream); try { for (ZipEntry entry = null; (entry = is.getNextEntry()) != null;) { OutputStream os = new BufferedOutputStream(new FileOutputStream(entry.getName())); try { final int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; for (int readBytes = -1; (readBytes = is.read(buffer, 0, bufferSize)) > -1;) { os.write(buffer, 0, readBytes); } os.flush(); } finally { os.close(); } } } finally { is.close(); } } 
+5
source

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


All Articles