- 浏览: 86418 次
- 性别:
- 来自: 上海
文章分类
最新评论
支持Window和Linux下tar.gz文件压缩
- 博客分类:
- CoreJava
最近做文件压缩,最后在Window下压缩解压后都可以,可是在Windows下压缩后到Linux下用命令解压(tar zxvf tarname.tar.gz -C unFolderName)就出现问题了,文件名包括在单独的文件中(05\foldername\file.txt)。
后来发现是压缩单独的文件时出错了,
另外就是,压缩文件夹和压缩单独文件分开
后来发现是压缩单独的文件时出错了,
TarArchiveEntry tae = new TarArchiveEntry(file, finalFileName);这样写就出现了以上错误,改成以下一个参数的就可以了
TarArchiveEntry tae = new TarArchiveEntry(finalFileName);
另外就是,压缩文件夹和压缩单独文件分开
private void compressPerDayFolder(String currentDay) throws IOException, CompressorException { // prepare current need to compress day source file folder StringBuffer sourceFileDirPath = new StringBuffer(compressMonthDirPath) .append("/").append(currentDay); String sourceFileDirPathStr = sourceFileDirPath.toString().replace("/", File.separator); File sourceFileDir = new File(sourceFileDirPathStr); // prepare current Day directory for following use compressDayDirPath = sourceFileDirPathStr; compressDayDirPathLengthAddOne = sourceFileDirPathStr.length() + 1; // prepare current day finally compressed file StringBuffer targetFileName = new StringBuffer("snapshot_archive_") .append(compressYear).append(compressMonth).append(currentDay) .append(".tar.gz"); StringBuffer targetFilePath = new StringBuffer(compressMonthDirPath) .append(File.separator).append(targetFileName); File targetFile = new File(targetFilePath.toString()); if (sourceFileDir.isDirectory()) { File[] sourceFileArr = sourceFileDir.listFiles(); // compress all files in this day folder packAndCompressSnapshotXmlFiles(sourceFileArr, targetFile); } else { getLogger().error("This is not folder: " + sourceFileDirPathStr); } } private void packAndCompressSnapshotXmlFiles(File[] sourceFileArr, File targetFile) throws IOException, CompressorException { FileOutputStream fileOutputStream = null; CompressorOutputStream gzippedOut = null; TarArchiveOutputStream taos = null; try { fileOutputStream = FileUtils.openOutputStream(targetFile); gzippedOut = new CompressorStreamFactory().createCompressorOutputStream( CompressorStreamFactory.GZIP, fileOutputStream); taos = new TarArchiveOutputStream(gzippedOut); // iteration all files in this day folder to compress for (File hourDirFile : sourceFileArr) { compressSnapshotFolders(hourDirFile, taos); } } catch (IOException ioe) { throw ioe; } catch (CompressorException ce) { throw ce; } finally { if (taos != null) { try { taos.close(); } catch (IOException e) { } } if (gzippedOut != null) { try { gzippedOut.close(); } catch (IOException e) { } } } } private void compressSnapshotFolders(File file, TarArchiveOutputStream taos) throws IOException { if (file.isDirectory()) { parkAndCompressPerDir(file, taos); File[] subFileArr = file.listFiles(); for (File subFile : subFileArr) { compressSnapshotFolders(subFile, taos); } } else { parkAndCompressPerFile(file, taos); } } private void parkAndCompressPerFile(File file, TarArchiveOutputStream taos) throws IOException { // substring the file name path(forget all before the day folder, not // including the day and file separator) String fileAbstPath = file.getAbsolutePath(); String finalFileName = fileAbstPath.substring(fileAbstPath .indexOf(compressDayDirPath) + compressDayDirPathLengthAddOne); // this file will in this folders if the name string including // current operation system file separator TarArchiveEntry tae = new TarArchiveEntry(finalFileName); tae.setSize(file.length()); taos.putArchiveEntry(tae); taos.write(FileUtils.readFileToByteArray(file)); taos.closeArchiveEntry(); } private void parkAndCompressPerDir(File file, TarArchiveOutputStream taos) throws IOException { // substring the file name path(forget all before the day folder, not // including the day and file separator) String fileAbstPath = file.getAbsolutePath(); String finalFileName = fileAbstPath.substring(fileAbstPath .indexOf(compressDayDirPath) + compressDayDirPathLengthAddOne); // this file will in this folders if the name string including // current operation system file separator TarArchiveEntry tae = new TarArchiveEntry(finalFileName + File.separator); taos.putArchiveEntry(tae); taos.closeArchiveEntry(); }
发表评论
-
Tomcat Servlet request.getRemoteAddr()所得值为0:0:0:0:0:0:0:1
2013-11-08 11:53 1506环境: tomcat5.6 JSP UTF-8 IP协 ... -
追踪应用客户端IP
2013-08-22 09:53 697参考: http://www.knowsky.co ... -
java多线程发送URL请求
2013-07-19 11:32 1137import java.io.Buffered ... -
java启用多进程调用某个类(是class文件)
2013-07-19 11:29 628import java.io.BufferedInpu ... -
请慎用java的File#renameTo(File)方法
2013-07-03 12:14 524转自: http://xiaoych.iteye.com/bl ... -
java从tar.gz中抽取某个文件
2013-07-01 15:03 1293File tarGzFile = new File(a ... -
在不同jar文件中有两个同名类是加载的哪一个已经classload
2013-06-24 09:33 1422在Eclipse中加载第一个(通常是lib下面的顺序,但并非一 ... -
Java泛型的使用及List排序
2013-04-19 09:56 943import java.util.ArrayLis ... -
java删除一个文件夹,包括该文件夹下的所有文件夹及文件
2013-04-10 17:42 732要删除的文件必须存在且是一个文件夹,可以根据自己的需求,在调用 ... -
java压缩文件生成XXX.tar.gz压缩包
2013-04-09 13:42 2197生成XXX.tar.gz压缩文件有两种方式,可以先打包后压缩, ... -
java按照编码读写文件
2013-04-07 09:28 765package d20130406; impor ... -
java字符串中显示双引号
2013-04-19 09:58 1174转: http://www.cnblogs.com/zhish ... -
java写入txt文件
2013-04-19 09:59 612用另一个构造方法FileWriter fileWriter=n ... -
读取文件方法大全
2013-04-25 11:51 260转自: http://www.cnblogs.com/love ... -
Java性能监测
2013-04-15 13:20 512通过jdk自带工具监测: -
Java 32bit JVM Xmx 参数大小限制
2013-03-07 13:26 1401转自: http://stackoverflow.com/qu ... -
Java模拟HTTP发送请求
2013-04-15 13:17 730package com.hp.pn.service; ... -
tomcat配置文件
2013-03-01 13:51 602转自: http://www.cnblogs.com/smil ... -
PermGen space错误解决方法,java.lang.OutOfMemoryError
2013-03-01 11:07 770转自: http://www.cnblogs.com/xwdr ... -
<context-param>与<init-param>的区别与作用
2013-02-28 09:41 540转载: http://www.cnblogs.com/hzj- ...
相关推荐
My_SQL第伍版 samp_db数据库tar.gz文件,方便大家学习下载在linux环境下学习使用,这个压缩文件是tar.gz格式,我也提供了一个zip格式(window环境下学习),自行选择哦,如有侵权,将及时删除!
内容概要:文件【jdk-8u201 windows和linux版本】解压后内含jdk-8u201-linux-x64.tar.gz、jdk-8u201-windows-x64.exe两个版本的安装包,可以在windows和linux环境进行jdk环境搭建 适合人群:常规Java开发人员以及...
可是Linux就不同了,他有.gz、.tar.gz、tgz、bz2、.Z、.tar等众多的压缩文件名,此外windows下的.zip和.rar也能在Linux下使用,不过在Linux使用.zip和.rar的人就太少了。本文就来对这些常见的压缩文件进行一番小结,...
UNIX/Linux主要版本、开源/自由软件、X Window、Linux应用领域 2、Linux系统安装 掌握 1)VMware简单使用 2)Linux系统安装 图形安装和字符安装,图形安装-语言中文,字符安装-语言英文,硬盘分区(重要:分区...
加载CDROM设备,这时如果进入 /mnt 目录下,你将会 发现多了一个文件:vmware-linux-tools.tar.gz.这就是WMWARE TOOLS的LINUX软件包,也就是我们刚才使用WINISO 打开LINUX.ISO文件所看到的. cp /mnt/vmware-linux-...
先用tar打包,然后再用gz压缩得到的文件 zip: 不同于gzip。尽管使用相似的算法,能够打包压缩多个文件。只是分别压缩文件。压缩率低于tar。 rar:打包压缩文件。最初用于DOS,基于window操作系统。 压缩率比zip高,...
当你下载到Iperf源码包后,在类unix系统上,需要先解压缩源码包,使用解压缩命令gzip或tar。解压后会产生新文件夹“Iperf-2.0.2”。Iperf2.0.2在以下操作系统中可以很好的被编译:Linux, SGI IRIX, HP-UX, ...
DbVisualizer是一款非常优秀的数据库管理工具。 亲测可用,在Mac环境中 10.0.10 成功,在Windows环境中原理一样。...Linux版本:http://www.dbvis.com/product_download/dbvis-10.0.10/media/dbvis_unix_10_0_10.tar.gz
运行dpkg -i kongdash-xyz-arch.deb Fedora和其他Linux发行版: 下载.tar.gz存档。 解压缩归档文件并运行install.sh脚本。tar -xvf kongdash-x.y.z-linux-arch.tar.gzcd kongdash-x.y.z-linux-arch./install.sh xyz...
这种软件安装包通常是用gzip压缩过的tar包(后缀名为.tar.gz)。 另一种是软件的可执行程序,你只要安装它就可以了。这种软件安装包通常是一个RPM包(RedHat Linux Packet Manager,就是RedHat的软件包管理器),...
我们使用SELF(Simple Embedded Linux Framework)来展示如何建立一个开发环境(包括通过NFS挂载的根文件系统)和一个嵌入式目标板配置(从基于busybox的ramdisk映像文件中运行)。 本文档不会给出如何把U-Boot或者...
Source tarball jhead-3.00.tar.gz (69 k) Pre-built Windows executable jhead.exe (166 k) Pre-built Linux executable (built on Centos) jhead (77 k) Pre-built OS-X Intel executable (version 2.97) jhead ...