The compressed data within any given file within a .zip can be extracted using Chilkat Zip, saved to disk, and then loaded into a Java byte array and inflated using Java.util.zip.Inflater. Here’s how:
First, here is sample C# code to load a .zip and save the compressed data from the first zipped file to disk:
Chilkat.Zip zip = new Chilkat.Zip();
zip.UnlockComponent("30-day trial");
zip.OpenZip("test.zip");
Chilkat.ZipEntry entry = zip.FirstEntry();
byte[] compressedData = entry.Copy();
// Write the compressed data to a file.
FileStream fs = File.Create("output.dat");
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(compressedData);
bw.Close();
Here’s the corresponding code using Java’s java.util.zip.Inflater to load the file and inflate:
File inputZipFile = new File("output.dat");
FileInputStream fIn = new FileInputStream(inputZipFile);
final byte[] bytes = new byte[(int) inputZipFile.length()];
fIn.read(bytes);
Inflater decompressor = new Inflater(true);
ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
decompressor.setInput(bytes);
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
bos.close();
byte[] originalData = bos.toByteArray();
Note: The Inflater class must be constructed using the constructor:
public Inflater(boolean nowrap)
Creates a new decompressor. If the parameter 'nowrap' is true then the ZLIB header
and checksum fields will not be used. This provides compatibility with the compression
format used by both GZIP and PKZIP.