[Java]GZIP工具類壓縮/解壓
阿新 • • 發佈:2021-12-21
/** * 解壓流和壓縮流 */ @Test public void GzipInput() throws IOException { String filePath = "/Users/yans/Desktop/zip/test.txt"; String fileOutPath = "/Users/yans/Desktop/zip/test.gz"; FileInputStream fin = new FileInputStream(filePath); GZIPOutputStream gzout = new GZIPOutputStream(new FileOutputStream(fileOutPath)); byte[] buf = new byte[1024 * 100]; int len; while ((len = fin.read(buf,0,buf.length)) != -1){ gzout.write(buf); } gzout.flush(); gzout.finish(); fin.close(); gzout.close(); } @Test public void GzipOutPut() throws IOException { String filePath = "/Users/yans/Desktop/zip/test.gz"; String fileOutPath = "/Users/yans/Desktop/zip/test.txt"; FileInputStream fin = new FileInputStream(filePath); GZIPInputStream gzin = new GZIPInputStream(fin); FileOutputStream fout = new FileOutputStream(fileOutPath); byte[] buf = new byte[1024 * 100]; int len; while ((len = gzin.read(buf,0,buf.length)) != -1){ fout.write(buf); } fout.flush(); fin.close(); gzin.close(); fout.close(); }