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.

 

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

相关文章
|
Apache
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
550 0
|
Java Apache Maven
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
1337 0
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory解决方法
|
5月前
|
Java 数据库连接 Apache
深入理解Apache Commons Pool2池化技术
深入理解Apache Commons Pool2池化技术
|
6月前
|
算法 Java Apache
Apache Commons
Apache Commons是一个开源项目,提供了一系列的工具和库,用于简化Java开发中的常见任务。
65 1
|
移动开发 前端开发 Java
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
102 0
|
Java 应用服务中间件 Apache
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
360 0
|
Java Apache
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
104 0
|
存储 Java Apache
java积累——apache commons fileupload 实现文件上传
java积累——apache commons fileupload 实现文件上传
365 0
|
编解码 算法 Apache
Apache Commons Codec:各种加密了解一下
Apache Commons Codec 简介 Apache Commons Codec: Apache Commons Codec (TM) software provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs. Apache Commons Codec(TM)软件提供常见编码器和解码器的实现,如Base64,Hex,Phonetic和URL。
2910 0
|
Apache
Apache Commons DbUtils工具使用
Apache Commons DbUtils工具使用
150 0
Apache Commons DbUtils工具使用

推荐镜像

更多
下一篇
无影云桌面