It's been a while since I've done Java I/O, and I'm not aware of the latest "right" ways to work with Zip and GZip files. I don't necessarily need a full working demo - I'm primarily looking for the right interfaces and methods to be using. Yes, I could look up any random tutorial on this, but performance is an issue (these files can get pretty big) and I do care about using the best tool for the job.
The basic process I'll be implementing:
The input files might be compressed and archived more than once. For example, the "full extraction" should take any of the following inputs (I'm not in control of these), and leave behind foo.txt
:
foo.txt.gz
foo.txt.zip
foo.txt.gz.zip
foo.txt.zip.gz
foo.txt.gz.gz.gz.zip.gz.zip.zip.gz.gz
Then, I might be left with foo.txt
, bar.mp3
, baz.exe
- so I would just add them all to a new zip file with some generic name.
Don't hold all this uncompressed data in memory, or you might run out of heap space. You need to stream the data out to file when uncompressing and then stream it back in from file when you want to create your final zip file.
I haven't done zipped files before, but here is an example which shows how to uncompress a gzipped file:
import java.io.*;
import java.util.zip.*;
//unzipping a gzipped file
GZIPInputStream in = null;
OutputStream out = null;
try {
in = new GZIPInputStream(new FileInputStream("file.txt.gz"));
out = new FileOutputStream("file.txt");
byte[] buf = new byte[1024 * 4];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (in != null)
try {
in.close();
}
catch (IOException ignore) {
}
if (out != null)
try {
out.close();
}
catch (IOException ignore) {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With