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?(转)的更多相关文章

  1. How to untar a TAR file using Apache Commons

    import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress ...

  2. 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 ...

  3. 如何解压POSIX tar archive文件

    下载了一个xxx.gz的文件,使用x xxx.gz(zsh的x插件,十分之好用,再也不用担心tar后面该加哪些参数了)的命令解压,然后出现了一个文件,本以为解压后是一个文件夹:然后一脸蒙逼~ 突然又想 ...

  4. Linux tar This does not look like a tar archive

    由于昨天公司内网服务器坏了,所以急需搭建新的Linux环境. 在安装maven时,使用tar 命令解压maven.tar.gz出现: tar :This does not look like a ta ...

  5. tarfile — Read and write tar archive files

    参考: https://docs.python.org/2/library/tarfile.html http://www.jianshu.com/p/bbad16822eab #解压文件tarfil ...

  6. 安卓项目中使用JSON引发的一个小错误 Multiple dex files define Lorg/apache/commons/collections/Buffer

    原因: 这里添加的jar包和android自带的jar产生了冲突

  7. 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,这个包被间接的被其他 ...

  8. java 压缩以及解压文件,有tar,zip,gz(gizp)和解压

    package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.Lis ...

  9. Java解压tar.Z文件(使用Apache Commons-compress)

    这里使用apache commons compress对.tar.Z格式文件进行解压. 对于一个文件test.tar.Z,我们可以将解压过程理解为: 将test.tar.Z解压为test.tar: 将 ...

随机推荐

  1. java--从控制台读入一些数据

    学一些东西应该,学以致用: 现在我开始使用流的办法从控制台读取数据 import java.io.*; public class Demo2{ public static void main(Stri ...

  2. 模拟QQ系统设置面板实现功能

    业务需求: 基于网盘客户端的实现,原有网盘的设置面板无论从界面显示还是从业务需求都不能满足我们的正常需求.当前的要求是,模拟QQ系统设置的面板实现当前我们网盘中的基本配置功能.在完成这篇文章时已将基本 ...

  3. HTTP1.1协议中文版-RFC2616

    转自:http://www.cnpaf.net/Class/HTTP/200811/23277.html 说明 本文档规定了互联网社区的标准组协议,并需要讨论和建议以便更加完善.请参考 “互联网官方协 ...

  4. Handler不同线程间的通信

    转http://www.iteye.com/problems/69457 Activity启动后点击一个界面按钮后会开启一个服务(暂定为padService),在padService中会启动一个线程( ...

  5. Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

    Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列

  6. c语言数组初始化问题

    2147483648字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素. charstr[10]={'I','','a','m','',‘h’,'a','p','p','y'}; 即把10 ...

  7. 第14周 项目三-OOP版电子词典

    做一个简单的电子词典.在文件dictionary.txt中,保存的是英汉对比的一个词典,词汇量近8000个,英文.中文释义与词性间用'\t'隔开. (1)编程序,由用户输入英文词.显示词性和中文释义. ...

  8. Outlook Express 收发邮件出现"0x800CCC0F"错误代码解决方法

    非常多网友在使用Outlook Express收发邮件的过程中,有时能够发送邮件,但出现不能接收信件的问题.每次在收邮件都会在某个地方停止不动,然后报超时,出现错误代码0x800CCC19或0x800 ...

  9. android 逆向project smail 语法学习

    众所周知,android 是开源的.如今市场上反编译别人的劳动果实的人也不少.所以我们也是有必要学习下smail语言,(就是androidproject反编译后出的语法语音),看看改怎么给我们的代码 ...

  10. RAC 11.2.0.4 安装 遇到 INS-06001

    今天安装11.2.0.4的grid软件,在配置passwordless SSH的时候,点击setup 出现此错误 开始网上搜了一把.说什么的都有,什么系统bug啊什么的 但是我另外一套rac却很正常就 ...