gzip压缩解压缩

package com.raise.raisestudy.zip;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class GzipUtils {

  /** 
     * 压缩文件或目录<br/> 
     * 将文件或者目录压缩为zip格式的压缩文件 
     * 
     * @param compressedFile 压缩后的文件 
     * @param preCompressFile   需要压缩的文件或目录路径 
     * @return true | false 
     */  
    public static boolean compress(String compressedFile, String preCompressFile)  
    {  
        boolean isSucc = true;  
        File inputFile = new File(preCompressFile);  
        ZipOutputStream out;  
        try  
        {  
            out = new ZipOutputStream(new FileOutputStream(compressedFile));  
            compress(out, inputFile, inputFile.getName());  
            out.close();  
        }  
        catch (Exception e)  
        {  
            isSucc = false;    
        }  
        return isSucc;  
    }  
      
    /** 
     * 递归压缩文件夹下的所有文件 
     * 
     * @param out 输出流 
     * @param file 需要压缩的文件 
     * @param base 压缩后文件的父目录 
     * @throws IOException 
     */  
    private static void compress(ZipOutputStream out, File file, String base)  
            throws IOException  
    {  
        if (file.isDirectory())  
        {  
            File[] fs = file.listFiles();  
            base += "/";  
            out.putNextEntry(new ZipEntry(base)); // 生成相应的目录  
            for (int i = 0; i < fs.length; i++)  
            {  
                // 对本目录下的所有文件对象递归遍历,逐个压缩  
                compress(out, fs[i], base + fs[i].getName());  
            }  
              
        }  
        else  
        {  
            out.putNextEntry(new ZipEntry(base));  
            InputStream is = new FileInputStream(file);  
            byte[] buf = new byte[1024];  
            int len = 0;  
            while ((len = is.read(buf)) != -1)  
            {  
                out.write(buf, 0, len);  
            }  
            is.close();  
        }  
    }  
      
    /** 
     * 解压缩文件<br/> 
     * 解压缩zip格式的文件 
     * @param zipFile 需要解压缩的文件 
     * @param desPath 解压后保存的目录 
     */  
    public static void decompress(String zipFile, String desPath) throws IOException 
    {  
        // 建立输出流,用于将从压缩文件中读出的文件流写入到磁盘  
        OutputStream out = null;  
        // 建立输入流,用于从压缩文件中读出文件  
        ZipInputStream is;  
        File dir = new File(desPath);  
        dir.mkdirs();  
            is = new ZipInputStream(new FileInputStream(zipFile));  
            ZipEntry entry = null;  
            while ((entry = is.getNextEntry()) != null)  
            {  
                File f = new File(dir, entry.getName());  
                if (entry.isDirectory())  
                {  
                    f.mkdir();  
                }  
                else  
                {  
                    // 根据压缩文件中读出的文件名称新建文件  
                    out = new FileOutputStream(f);  
                    byte[] buf = new byte[1024];  
                    int len = 0;  
                    while ((len = is.read(buf)) != -1)  
                    {  
                        out.write(buf, 0, len);  
                    }  
                    out.close();  
                }  
            }  
              
            is.close();  
    }  
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值