Skip to content

Instantly share code, notes, and snippets.

@bitristan
Created August 28, 2019 11:49
Show Gist options
  • Save bitristan/651d0e24162b982525c96159e03609a2 to your computer and use it in GitHub Desktop.
Save bitristan/651d0e24162b982525c96159e03609a2 to your computer and use it in GitHub Desktop.
Use okio to zip file.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.GzipSink;
import okio.Okio;
public class ZipUtil {
public static void zip(File source, File dest) throws IOException {
BufferedSource bufferedSource = null;
BufferedSink bufferedSink = null;
try {
bufferedSource = Okio.buffer(Okio.source(source));
bufferedSink = Okio.buffer(new GzipSink(Okio.sink(dest)));
int length;
byte[] buf = new byte[8192];
while ((length = bufferedSource.read(buf, 0, buf.length)) != -1) {
bufferedSink.write(buf, 0, length);
}
} finally {
IoUtils.closeQuietly(bufferedSource);
IoUtils.closeQuietly(bufferedSink);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment