我读了如何将文件附加到java中的tar存档? , 将文件附加到存档而不读取/重写整个存档,并将条目添加到tar文件而不覆盖其现有内容,但没有给出好的答案。 此外,我没有足够的声誉发表评论。 所以我在这里创建了一个新问题。
有没有办法在tar存档中附加文件? 如果文件已存在,我想替换它。
我已经开始编写以下方法,但是当添加文件时,它会删除存档内容。 我没有在apache compress网站上找到任何示例。
static final Logger LOG = Logger.getLogger(ShellClient.class); public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws 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; } TarArchiveOutputStream aos = null; OutputStream out = null; try { out = new FileOutputStream(tarFile); aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out); // create a new entry final TarArchiveEntry entry = new TarArchiveEntry(fileToAdd); entry.setSize(fileToAdd.length()); // add the entry to the archive aos.putArchiveEntry(entry); InputStream is = new FileInputStream(fileToAdd); final int byteCopied = IOUtils.copy(is, aos); if (LOG.isDebugEnabled()) { LOG.debug(byteCopied+" bytes inserted in the tar archive from "+fileToAdd); } is.close(); aos.closeArchiveEntry(); aos.finish(); aos.flush(); aos.close(); out.flush(); out.close(); } catch (ArchiveException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(aos); IOUtils.closeQuietly(out); FileUtils.deleteQuietly(fileToAdd); } }I read How do I append files to a tar archive in java?, append files to an archive without reading/rewriting the whole archive and Add an entry to a tar file without overwriting its existing contents but no good answer was given. Moreover I do not have enough reputation to post comments. So I created a new question here.
Is there a way to append a file in a tar archive? I would like to replace a file if it exists already.
I have started to write the following method but when the file is added, it erases the archive content. I didn't find any example on the apache compress website.
static final Logger LOG = Logger.getLogger(ShellClient.class); public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws 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; } TarArchiveOutputStream aos = null; OutputStream out = null; try { out = new FileOutputStream(tarFile); aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out); // create a new entry final TarArchiveEntry entry = new TarArchiveEntry(fileToAdd); entry.setSize(fileToAdd.length()); // add the entry to the archive aos.putArchiveEntry(entry); InputStream is = new FileInputStream(fileToAdd); final int byteCopied = IOUtils.copy(is, aos); if (LOG.isDebugEnabled()) { LOG.debug(byteCopied+" bytes inserted in the tar archive from "+fileToAdd); } is.close(); aos.closeArchiveEntry(); aos.finish(); aos.flush(); aos.close(); out.flush(); out.close(); } catch (ArchiveException e) { LOG.error(e.getMessage(), e); } catch (IOException e) { LOG.error(e.getMessage(), e); } finally { IOUtils.closeQuietly(aos); IOUtils.closeQuietly(out); FileUtils.deleteQuietly(fileToAdd); } }最满意答案
最后,我成功地使用这篇文章 。
我创建了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); } }Finally I succeeded to do it using this post.
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); } }更多推荐
发布评论