i try to compress large video file with more than 100Mb in size.
public static void compress(File input, File output) throws IOException {
InputStream fis = new FileInputStream(input);
byte[] bFile = IOUtils.toByteArray(fis);
FileOutputStream fos = new FileOutputStream(output);
GZIPOutputStream gzipStream = new GZIPOutputStream(fos);
try {
gzipStream.write(bFile);
// IOUtils.copy(fis, gzipStream);
} finally {
gzipStream.close();
fis.close();
fos.close();
}
}
every time i got outofmemoryerror.
You should copy the data progressively and you won't run out of memory.
public static void compress(File input, File output) throws IOException {
try(InputStream in = new FileInputStream(input);
OutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
byte[] bytes = new byte[4096];
for(int len; (len = in.read(bytes)) > 0; )
out.write(bytes, 0, len);
}
}
This will use about 4 KB as a buffer at once, regardless of the size of the file. (I suspect the GZIP uses about the same to do it's work)
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