Skip to content

Instantly share code, notes, and snippets.

@cstrap
Last active August 29, 2015 14:04
Show Gist options
  • Save cstrap/a38d238e630fa243f176 to your computer and use it in GitHub Desktop.
Save cstrap/a38d238e630fa243f176 to your computer and use it in GitHub Desktop.
Gzip file in java
public static void gzipFile(String source, String destination, boolean delete) {
try {
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(format("%s.gz", destination));
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
gzipOS.write(buffer, 0, len);
}
gzipOS.close();
fos.close();
fis.close();
if (delete) {
new File(source).delete();
}
} catch (IOException e) {
err.println(e.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment