Created
August 28, 2019 11:49
-
-
Save bitristan/651d0e24162b982525c96159e03609a2 to your computer and use it in GitHub Desktop.
Use okio to zip file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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