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

时间: 2024-10-13 06:48:49

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.archivers.tar.TarArchiveInputStream; import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.compres

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-20-0-linux-x86-64.zip tar: This does not look like a tar archive tar: Skipping to next header tar: Exiting with failure status due to previous errors 解决办法: unzip bcl2fastq2-v2-20-0-tar.zip 生成 bcl2fastq2-v2.20

tar: This does not look like a tar archive

报错信息如下: # tar -zxvf extmail-1.2.tar.gz tar: Skipping to next header tar: Exiting with failure status due to previous errors 解决: # file extmail-1.2.tar.gz extmail-1.2.tar.gz: gzip compressed data, from Unix # gzip -d extmail-1.2.tar.gz # tar -xf extma

Multiple dex files define Lorg/apache/commons/codec/Decoder; 我的解决方案

1.将  Java Build Path -> Libraries  中的所有jar Remove掉 点右边的 Add External JARs 找到jar的位置添加 2.Properties -> Android -> Library 中所有引用Remove掉 再依次添加 3.clean up

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

package com.yabsz.decompCompr; import java.io.File; import java.util.ArrayList; import java.util.List; public class main { public static void main(String[] args) { //需要下载commons-net-ftp-2.0.jar包下载地址:http://download.csdn.net/detail/u010696272/8006739

【转】JAVA解压.TAR.Z及.ZIP文件

 解压.ZIP文件 package app.qdupr.Method; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;

【java】 java 解压tar.gz读取内容

package com.xwolf.stat.util; import com.alibaba.druid.util.StringUtils; import com.alibaba.fastjson.JSON; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compr

贴一段java读取hdfs 解压gz zip tar.gz保存到hdfs的代码

package main.java; import java.io.*;import java.util.LinkedList;import java.util.List;import java.util.zip.*; import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.a

使用commons-compress操作zip文件(压缩和解压缩)

http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可以操作ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200 and bzip2格式的文件,功能比较强大. 在这里写两个用Commons Compress把文件压缩成zip和从zip解压缩的方法. 直接贴上工具类代码: /** * Zip文件工具类 * @a