Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Apache Commons Compress将文件附加到.tar存档?_Java_Compression_Archive_Tar_Apache Commons Compress - Fatal编程技术网

Java 如何使用Apache Commons Compress将文件附加到.tar存档?

Java 如何使用Apache Commons Compress将文件附加到.tar存档?,java,compression,archive,tar,apache-commons-compress,Java,Compression,Archive,Tar,Apache Commons Compress,我读了,但没有给出好的答案。此外,我没有足够的声誉发表评论。所以我在这里提出了一个新问题 有没有办法将文件附加到tar存档中?如果文件已经存在,我想替换它 我已经开始编写以下方法,但当添加文件时,它会删除存档内容。我在网上找不到任何例子 最后,我成功地使用了它 我创建了一个tar归档的副本,并将整个内容复制到它。然后我删除了旧的tar档案 public void appendFileInTarArchive(String tarPath, String tarFileName, String f

我读了,但没有给出好的答案。此外,我没有足够的声誉发表评论。所以我在这里提出了一个新问题

有没有办法将文件附加到tar存档中?如果文件已经存在,我想替换它

我已经开始编写以下方法,但当添加文件时,它会删除存档内容。我在网上找不到任何例子


最后,我成功地使用了它

我创建了一个tar归档的副本,并将整个内容复制到它。然后我删除了旧的tar档案

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);
    }
}
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);
    }
}