Skip to content

Instantly share code, notes, and snippets.

@berlinbrown
Created July 8, 2011 01:36
Show Gist options
  • Save berlinbrown/1070925 to your computer and use it in GitHub Desktop.
Save berlinbrown/1070925 to your computer and use it in GitHub Desktop.
Example Gzip Code in Java2
/*
Unzip a gz file in Java:
You could probably modify to do a zip.
*/
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
public class UnzipWriter {
private final String filename;
private final FileWriterConf conf;
private final ServerDirConf dirConf;
private int bufferSize = 2048;
public UnzipWriter(final String filename, final FileWriterConf conf, final ServerDirConf dir) {
this.filename = filename;
this.conf = conf;
this.dirConf = dir;
}
public boolean unzipWrite() {
final long tstart = System.currentTimeMillis();
GZIPInputStream zipfile = null;
OutputStream out = null;
boolean success = false;
try {
final File f = new File(this.filename);
final String outputfilename = new CustomFilenameMangler(this.conf, this.dirConf).parseZippedFilenameMangle(this.filename);
final File fout = new File(outputfilename);
if (!f.exists()) {
return false;
}
zipfile = new GZIPInputStream(new FileInputStream(f));
out = new FileOutputStream(fout);
byte[] buf = new byte[bufferSize];
int len;
while ((len = zipfile.read(buf)) > 0) {
out.write(buf, 0, len);
} // End of the While //
success = true;
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // End of the try - catch //
final long tdiff = System.currentTimeMillis() - tstart;
System.out.println("Unzip file="+this.filename + " procTime=" + tdiff);
return success;
}
public boolean deleteArchiveFile() {
final File f = new File(this.filename);
return f.delete();
}
} // End of the Class //
@berlinbrown
Copy link
Author

I hope this is assigned to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment