/**
* 压缩文件成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压缩输出流
TarArchiveOutputStream tout=new TarArchiveOutputStream(fout);
for(int i=0;i<length;i++)
{
String filename=srcPath.getPath()+File.separator+files[i].getName();
//打开需压缩文件作为文件输入流
FileInputStream fin=new FileInputStream(filename); //filename是文件全路径
TarArchiveEntry tarEn=new TarArchiveEntry(files[i]); //此处必须使用new TarEntry(File file);
tarEn.setName(files[i].getName()); //此处需重置名称,默认是带全路径的,否则打包后会带全路径
tout.putArchiveEntry(tarEn);
int num;
while ((num=fin.read(buf, 0, 1024)) != -1)
{
tout.write(buf,0,num);
}
tout.closeArchiveEntry();
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, 0, 1024)) != -1)
{
gzout.write(buf,0,len);
}
gzout.close();
gzFile.close();
tarin.close(); File f = new File(targzipFilePath);
f.deleteOnExit();
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
}

java tar.gz文件生成的更多相关文章

  1. 使用Java API进行tar.gz文件及文件夹压缩解压缩

    在java(JDK)中我们可以使用ZipOutputStream去创建zip压缩文件,(参考我之前写的文章 使用java API进行zip递归压缩文件夹以及解压 ),也可以使用GZIPOutputSt ...

  2. tar.gz文件命名和压缩解压方法

    tar.gz文件命名 tar这是文件打成一个包,无压缩; gz同gzip标记的包.tar文件压缩; 所以它成为一个.tar.gz档 压缩 # tar cvfz backup.tar.gz /xxx/ ...

  3. 解压.zip,.tar.gz文件到指定目录,重命名文件

    1.解压文件到指定目录 /** * 解压文件到指定目录 * zipFile:要解压的文件 * descDir:解压到哪个文件 * */ @SuppressWarnings("rawtypes ...

  4. tar.gz文件命名及压缩解压方法

    tar.gz文件命名 tar是把文件打成一个包,并不压缩; gz是用gzip把打成包的.tar文件压缩; 所以成了一个.tar.gz的文件 压缩 # tar cvfz backup.tar.gz /x ...

  5. linux commands - 一次性解压多个tar.gz文件

    tar -zxvf list所有tar.gz文件,然后利用xargs将其作为参数传给tar命令.-n 1表示每次传一个参数. xargs: https://www.cnblogs.com/wangqi ...

  6. windows下python的tar.gz文件安装

    windows下下载了django,PIL,web.py发现都是tar.gz格式的文件,网上查找也非常系统的方法,总结一下其他大神的方法,归纳于此. 首先下载tar.gz文件,比如web.py,下载后 ...

  7. [Linux] 解压tar.gz文件,解压部分文件

    遇到数据库无法查找问题原因,只能找日志,查找日志的时候发现老的日志都被压缩了,只能尝试解压了   数据量比较大,只能在生产解压了,再进行查找 文件名为*.tar.gz,自己博客以前记录过解压方法: h ...

  8. 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法

    解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...

  9. 把.zip文件转化为.tar.gz文件

    工作中正好用到上传tar.gz文件,没有现成的转换工具,就写了方法转换 #encoding: utf-8import osimport tarfileimport zipfileimport osim ...

随机推荐

  1. Linux SVN 服务器

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository) 中 ...

  2. FileInputStream 和 FileOutputStream

    简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...

  3. 【转】Hudson插件Email-Ext邮件模板时间格式化的解决方法

    原文地址:http://www.cnblogs.com/haycco/archive/2012/03/20/3031397.html 最近因对Hudson版本进行了升级为2.2.0,所以各方面都在搞项 ...

  4. jQuery里面ajax请求的封装

    为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码: function api_request(name, params, cb, scope, async, el) { if ( ...

  5. springMVC前后台交互

    后台返回json对象: package com.sawshaw.controller; import org.springframework.stereotype.Controller; import ...

  6. python开发环境搭建(windows+python2.7.5+django1.5.4)【原创】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  7. canvas API笔记

    HTML5添加的最受欢迎的功能就是<canvas>元素,这个元素负责在页面的指定区域通过js动态地在这个区域绘制图形. <canvas> 由几组API构成. 准备: <c ...

  8. HDU 2602 - Bone Collector - [01背包模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2602 Many years ago , in Teddy’s hometown there was a ...

  9. hadoop第一部分-安装、测试

    一.hadoop安装(本地模式及伪分布式安装) hadoop历史版本下载网站:http://archive.apache.org/dist/运行模式:    本地模式    yarn模式 hadoop ...

  10. c# listView中列宽随列中的内容自动调整

    1.创建一个类 ListViewAutoChange  作用:自动调整ListView的列宽 代码如下:(内容很容易理解,没加注释) using System; using System.Collec ...