apache - How to gzip file in place replacement Java -
i have .xlsx file (or file) gzip. able gzip file have problem of trying in place. meaning replace original file gzipped version of file.
here code:
public static void main(string[] args) throws ioexception { file file = new file("test.xlsx"); file gfile = new file(file.getabsolutepath()+".gz"); if(!file.exists()) { system.err.println("input tax file did not exist!"); } fileinputstream fis = new fileinputstream(file); fileoutputstream fos = new fileoutputstream(gfile); gzipoutputstream gos = new gzipoutputstream(fos); gzipreplace(fis, gos); } private static void gzipreplace(inputstream is, outputstream os) { int onebyte; try { while( (onebyte = is.read()) != -1 ) { os.write(onebyte); } os.close(); is.close(); } catch (exception e){ system.err.println(e.getstacktrace()); } }
how can in-place replacement of uncompressed file gzipped one?
just use file.delete()
on original file after successful compression , writing of gzip file.
you must careful not delete original file until new compressed file written , closed. otherwise setting lose data.
Comments
Post a Comment