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实现AES的加密和解密算法

    原文 JAVA实现AES的加密和解密算法 import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import ja ...

  2. 基于visual Studio2013解决C语言竞赛题之0905文件读写显示

       题目

  3. qingshow “不积跬步无以至千里,不积小流无以成江海”。--荀子《劝学篇》 用tomcat+花生壳搭建自己的web服务器+域名(参考)

    链接地址:http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html 用tomcat搭建web服务器 目标:免费拥有自己的网站及域名 ...

  4. .Net 社区虚拟大会”(dotnetConf)

    .Net 社区虚拟大会”(dotnetConf) “.Net 社区虚拟大会”(dotnetConf) 2016 今天凌晨在Channel9 上召开,在Scott Hunter的30分钟的 Keynot ...

  5. ADO.NET 操作数据库 --- 01 简单封装

    由于我是Java转的C#开始的时候就用的NHihernate,和EF 对ADO.NET使用较少,现在封装一个ADO.NET的工具类来实现数据库的操作,比较简单,望大家多多提意见. 如果大家有什么学习中 ...

  6. 每周日与周四《红酒屋》探戈舞会"Wine Bar" Milonga_原生态拉丁_新浪博客

    每周日与周四<红酒屋>探戈舞会"Wine Bar" Milonga_原生态拉丁_新浪博客     每周日与周四<红酒屋>探戈舞会"Wine Bar ...

  7. Oracle、DB2、MySql、SQLServer JDBC驱动

    四种数据库JDBC驱动,还列出了连接的Class驱动名和Url Pattern,DB2包括Type 2.Type 3和Type 4三种模式.注意驱动包名称的大小写. Oralce连接驱动包名和URL ...

  8. Spring Tool Suite(简称STS)针对SimpleDateFormat.pase函数的实参值不做检验,异常直接默认值之

    Spring Tool Suite(简称STS)是 Spring 团队开发的一款基于Eclipse的IDE,旨在简化开发Spring MVC 应用的流程.可以自动生成spring相关的配置文件.比如a ...

  9. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  10. [置顶] Android下实现自动关机的方法总结

    最近在网上看了一些Android下实现自动关机的方法,有的不行,有的只适用一些机型,有的适用于大部分机型,笔者在此总结一下 法一: Intent newIntent = new Intent(Inte ...