zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    开源监控软件之争
    为什么很多公司都自主开发监控系统?
    为 UWP 应用提供的 .NET 网络 API
    亲,根据二八定律,你的监控工具可能白装了哦
    PHP7正式版测试,性能惊艳!
    Java Web 前端高性能优化(一)
    天下武功无坚不破,唯快不破!
    告警信息大爆炸,运维解放秘籍!
    第33节:Java面向对象中的异常
    第33节:Java面向对象中的异常
  • 原文地址:https://www.cnblogs.com/softidea/p/4273097.html
Copyright © 2011-2022 走看看