gpt4 book ai didi

java - 在不覆盖其现有内容的情况下向 tar 文件添加条目

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:34:14 25 4
gpt4 key购买 nike

我需要将配置文件添加到现有的 tar 文件中。我正在使用 apache.commons.compress 库。以下代码片段正确添加了条目,但覆盖了 tar 文件的现有条目。

public static void injectFileToTar () throws IOException, ArchiveException {
String agentSourceFilePath = "C:\\Work\\tar.gz\\";
String fileToBeAdded = "activeSensor.cfg";
String unzippedFileName = "sample.tar";

File f2 = new File(agentSourceFilePath+unzippedFileName); // Refers to the .tar file
File f3 = new File(agentSourceFilePath+fileToBeAdded); // The new entry to be added to the .tar file

// Injecting an entry in the tar
OutputStream tarOut = new FileOutputStream(f2);
TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", tarOut);
TarArchiveEntry entry = new TarArchiveEntry(fileToBeAdded);
entry.setMode(0100000);
entry.setSize(f3.length());
aos.putArchiveEntry(entry);
FileInputStream fis = new FileInputStream(f3);
IOUtils.copy(fis, aos);
fis.close();
aos.closeArchiveEntry();
aos.finish();
aos.close();
tarOut.close();

在检查 tar 时,只找到“activeSensor.cfg”文件,并且发现 tar 的初始内容丢失。 “模式”设置不正确吗?

最佳答案

问题是 TarArchiveOutputStream 不会自动读取现有存档,这是您需要做的事情。类似的东西:

CompressorStreamFactory csf = new CompressorStreamFactory();
ArchiveStreamFactory asf = new ArchiveStreamFactory();

String tarFilename = "test.tgz";
String toAddFilename = "activeSensor.cfg";
File toAddFile = new File(toAddFilename);
File tempFile = File.createTempFile("updateTar", "tgz");
File tarFile = new File(tarFilename);

FileInputStream fis = new FileInputStream(tarFile);
CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

FileOutputStream fos = new FileOutputStream(tempFile);
CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

// copy the existing entries
ArchiveEntry nextEntry;
while ((nextEntry = ais.getNextEntry()) != null) {
aos.putArchiveEntry(nextEntry);
IOUtils.copy(ais, aos, (int)nextEntry.getSize());
aos.closeArchiveEntry();
}

// create the new entry
TarArchiveEntry entry = new TarArchiveEntry(toAddFilename);
entry.setSize(toAddFile.length());
aos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(toAddFile), aos, (int)toAddFile.length());
aos.closeArchiveEntry();

aos.finish();

ais.close();
aos.close();

// copies the new file over the old
tarFile.delete();
tempFile.renameTo(tarFile);

一些注意事项:

  • 此代码不包含任何异常处理(请添加适当的 try-catch-finally block )
  • 此代码不处理大小超过 2147483647 (Integer.MAX_VALUE) 的文件,因为它仅将文件大小读取为整数精度字节(请参阅转换为 int)。不过,这不是问题,因为 Apache Compress 无法处理超过 2 GB 的文件。

关于java - 在不覆盖其现有内容的情况下向 tar 文件添加条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10006781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com