把一个文件夹下的文件先打包成一个tar文件,然后再用gzip压缩。

 /**  
    * 压缩文件成Gzip格式,Linux上可使用  
 * 压缩文件夹生成后缀名为".gz"的文件并下载  
 * @param folderPath,要压缩的文件夹的路径  
 * @param zipFilePath,压缩后文件的路径  
 * @param zipFileName,压缩后文件的名称  
 * @throws BizException   
 * */      
public static void CompressedFiles_Gzip(String folderPath, String  targzipFilePath, String targzipFileName)    
{   
    File srcPath =new File(folderPath);   
    int length=srcPath.listFiles().length;   
    byte[] buf = new byte[1024]; //设定读入缓冲区尺寸    
    File[] files  =  srcPath.listFiles();   
    try    
    {    
        //建立压缩文件输出流    
        FileOutputStream fout=new FileOutputStream(targzipFilePath);   
        //建立tar压缩输出流    
        TarOutputStream tout=new TarOutputStream(fout);   
        for(int i=0;i<length;i++)   
        {   
           String filename=srcPath.getPath()+File.separator+files[i].getName();   
            //打开需压缩文件作为文件输入流    
            FileInputStream fin=new FileInputStream(filename);   //filename是文件全路径   
            TarEntry tarEn=new TarEntry(files[i]); //此处必须使用new TarEntry(File file);   
            tarEn.setName(files[i].getName());  //此处需重置名称,默认是带全路径的,否则打包后会带全路径   
            tout.putNextEntry(tarEn);     
            int num;    
            while ((num=fin.read(buf)) != -1)    
            {    
                tout.write(buf,0,num);    
            }    
            tout.closeEntry();   
            fin.close();    
        }   
        tout.close();      
        fout.close();      
           
        //建立压缩文件输出流    
        FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");   
        //建立gzip压缩输出流    
        GZIPOutputStream gzout=new GZIPOutputStream(gzFile);   
        //打开需压缩文件作为文件输入流    
        FileInputStream tarin=new FileInputStream(targzipFilePath);   //targzipFilePath是文件全路径   
        int len;    
        while ((len=tarin.read(buf)) != -1)    
        {    
            gzout.write(buf,0,len);    
        }    
        gzout.close();   
        gzFile.close();    
        tarin.close();    
    }catch(FileNotFoundException e)   
    {   
        System.out.println(e);    
    }catch(IOException e)   
    {   
        System.out.println(e);    
    }   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值