How to append files to a .tar archive using Apache Commons Compress?(转)
I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar archive.
public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws AuthenticationException, IOException {
if (tarPath == null || tarFileName == null || tarFileName.isEmpty()) {
LOG.warn("The path or the name of the tar archive is null or empty.");
return;
}
final File tarFile = new File(tarPath, tarFileName);
final File fileToAdd = new File(tarPath, file2WriteName);
FileUtils.write(fileToAdd, file2WriteContent);
if (file2WriteName == null || file2WriteName.isEmpty()) {
LOG.warn("The name of the file to append in the archive is null or empty.");
return;
}
ArchiveStreamFactory asf = new ArchiveStreamFactory();
File tempFile = new File(tarPath, "tmpTar.tar");
tempFile.createNewFile();
try {
FileInputStream fis = new FileInputStream(tarFile);
ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
FileOutputStream fos = new FileOutputStream(tempFile);
ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);
// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = ais.getNextEntry()) != null) {
aos.putArchiveEntry(nextEntry);
IOUtils.copy(ais, aos);
aos.closeArchiveEntry();
}
// create the new entry
TarArchiveEntry entry = new TarArchiveEntry(file2WriteName);
entry.setSize(fileToAdd.length());
aos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(fileToAdd), aos);
aos.closeArchiveEntry();
aos.finish();
ais.close();
aos.close();
// copies the new file over the old
tarFile.delete();
tempFile.renameTo(tarFile);
} catch (ArchiveException e) {
LOG.error(e.getMessage(), e);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
FileUtils.deleteQuietly(fileToAdd);
}
}
http://stackoverflow.com/questions/12890508/how-to-append-files-to-a-tar-archive-using-apache-commons-compress?rq=1
How to append files to a .tar archive using Apache Commons Compress?(转)的更多相关文章
- How to untar a TAR file using Apache Commons
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress ...
- tar: This does not look like a tar archive tar: Skipping to next header tar: Exiting with failure status due to previous errors
解压一个.tar.zip文件时报错 tar -zxvf bcl2fastq2-v2---linux-x86-.zip tar: This does not look like a tar archiv ...
- 如何解压POSIX tar archive文件
下载了一个xxx.gz的文件,使用x xxx.gz(zsh的x插件,十分之好用,再也不用担心tar后面该加哪些参数了)的命令解压,然后出现了一个文件,本以为解压后是一个文件夹:然后一脸蒙逼~ 突然又想 ...
- Linux tar This does not look like a tar archive
由于昨天公司内网服务器坏了,所以急需搭建新的Linux环境. 在安装maven时,使用tar 命令解压maven.tar.gz出现: tar :This does not look like a ta ...
- tarfile — Read and write tar archive files
参考: https://docs.python.org/2/library/tarfile.html http://www.jianshu.com/p/bbad16822eab #解压文件tarfil ...
- 安卓项目中使用JSON引发的一个小错误 Multiple dex files define Lorg/apache/commons/collections/Buffer
原因: 这里添加的jar包和android自带的jar产生了冲突
- The type org.apache.commons.cli.Options cannot be resolved. It is indirectly referenced from required .class files
在搭建好Hadoop Eclipse开发环境后,编写map-reduce,遇到如下的问题: 从字面上可以看出,工程缺少org.apache.commons.cli.Options,这个包被间接的被其他 ...
- java 压缩以及解压文件,有tar,zip,gz(gizp)和解压
package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.Lis ...
- Java解压tar.Z文件(使用Apache Commons-compress)
这里使用apache commons compress对.tar.Z格式文件进行解压. 对于一个文件test.tar.Z,我们可以将解压过程理解为: 将test.tar.Z解压为test.tar: 将 ...
随机推荐
- EF+jQueryUI前后端分离设计
开源项目练习EF+jQueryUI前后端分离设计 最近大家流行把项目开源,我也来玩玩.只是开源公司项目不好,小弟只好从公司项目经验上另外弄出一套练习开源给大家. 这个项目可以做简单的团队任务系统( ...
- 基于visual Studio2013解决C语言竞赛题之0806平均分
题目
- shapefile 编码错误问题解决 Wrong codepage of shapefile Warning 1: One or several characters couldn't be converted correctly from UTF-8 to ISO-8859-1.
linux下运行,因为大部分shapefile 文件,在使用时都没有指定字符集,所以qgis只能从环境变量中获取设置环境变量中获取SHAPE_ENCODING. 目前唯一的解决办法就是设置环境变量 $ ...
- linux命令: mount
mount使用示例: #mkdir -p /mnt/usbhd1 注:建立目录用来作挂接点(mount point) #mount -t ntfs /dev/sdc1 /mnt/usbhd1 (-t ...
- 幻世(OurDream)2D图形引擎使用教程8——处理操作输入(2)
声明:本教程版权归Lizcst Software Lab所有,欢迎转载,但是转载必须保留本段声明文字,并注明文章来源:http://blog.csdn.net/kflizcst 谢谢合作! 今天的教程 ...
- 如何在VC中显示透明背景位图
简单的调用系统API. Windows NT/2000/XP: Included in Windows 2000 and later.Windows 95/98/Me: Included in Win ...
- 对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点-----层序遍历的应用题
题目:对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点:如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL:给出程序实现,并分析时间复杂度和空间复杂度. ...
- iframe间的通信
父框架 <body></body> <script type="text/javascript"> document.domain = '100 ...
- 菜鸟级springmvc+spring+mybatis整合开发用户登录功能(上)
由于本人愚钝,整合ssm框架真是费劲了全身的力气,所以打算写下这篇文章,一来是对整个过程进行一个回顾,二来是方便有像我一样的笨鸟看过这篇文章后对其有所帮助,如果本文中有不对的地方,也请大神们指教. 一 ...
- spring mvc MultipartFile 上传文件错误解决
Field error in object 'xxxx' on field 'xxxx': rejected value [20129259128131.jpg]; codes [typeMismat ...