[Java]GZIP工具类压缩/解压

/**
     * 解压流和压缩流
     */
    @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();

    }
posted @ 2021-12-21 15:38  YanSss  阅读(598)  评论(0编辑  收藏  举报