把一个文件夹下的文件先打包成一个tar文件,然后再用gzip压缩。
2011-08-01 20:56
471 查看
/**
* 压缩文件成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);
}
}
* 压缩文件成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);
}
}
相关文章推荐
- linux的tar命令详情;linux多个文件压缩打包到一个压缩文件
- 文件与文件系统的压缩与打包 tar gzip bzip2
- Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接
- linux下的文件压缩与打包,compress,gzip,zcat,bzip2,bzcat,tar,find,tarfile,tarball
- linux笔记 文件的压缩与解压缩gzip、bzip2、xz、zip&unzip、tar打包、tar打包和压缩并用
- Unity LZMA GZIP 压缩、解压文件 和 打包文件夹 (支持进度回调)
- linux下的文件压缩与打包,compress,gzip,zcat,bzip2,bzcat,tar,find,tarfile,tarball
- [php]压缩类(archive,支持TAR/GZIP/BZIP2/ZIP),使用方便,一个文件
- linux命令五十七之tar命令;linux多个文件压缩打包到一个压缩文件
- AIX 文件 打包 与 压缩 tar gzip compress 的使用
- Unity LZMA GZIP 压缩、解压文件 和 打包文件夹 (支持进度回调)
- Linux系统学习第四章:文件压缩、打包与备份(一)压缩与打包:gzip、gcat、bzip2、bzcat、tar
- Unity LZMA GZIP 压缩、解压文件 和 打包文件夹 (支持进度回调)
- 指令篇:文件与文件系统的压缩与解压与打包(归档)与解压___gzip、zcat;bzip2、bzcat;zip; tar
- 使用tar和gzip对某文件夹内的某类型文件进行压缩与解压缩
- linux下的文件压缩与打包---compress,gzip,zcat,bzip2,bzcat,tar,find,tarfile,tarball
- Linux系统学习第四章:文件压缩、打包与备份(一)压缩与打包:gzip、gcat、bzip2、bzcat、tar
- 针对一个文件夹压缩成zip文件
- 如何压缩多个文件/文件夹(GZipStream and C#)
- 将文件树归档到一个压缩的tar文件中