TarArchiver implements FilesArchiver { @Override public <T> T appendFile(String path, FileAppender<T> appender) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); T result = appender.append(baos); TarArchiveEntry entry = new TarArchiveEntry(path); entry.setSize(baos.size()); synchronized (this) { tarOutput.putArchiveEntry(entry); tarOutput.write(baos.toByteArray()); tarOutput.closeArchiveEntry(); } return result; } protected  TarArchiver(TarArchiveOutputStream tarOutput);  TarArchiver(File tarFile); @Override T appendFile(String path, FileAppender<T> appender); @Override T appendBigFile(String path, FileAppender<T> appender); @Override void appendFile(String path, File file); @Override synchronized void close();  }
@Test public void testAppendFileSystem() throws Exception { File inFile = File.createTempFile("tarArchiverTest", ".txt"); try { final byte[] content = "Hello".getBytes(); try (FileOutputStream out = new FileOutputStream(inFile)) { out.write(content); } File tempFile = File.createTempFile("tarArchiverTest", ".tar"); try { try (TarArchiver instance = new TarArchiver(tempFile)) { instance.appendFile(inFile.getName(), inFile); } try (TarArchiveInputStream in = new TarArchiveInputStream( new FileInputStream(tempFile))) { ArchiveEntry nextEntry = in.getNextEntry(); String name = nextEntry.getName(); assertEquals(inFile.getName(), name); byte[] buff = new byte[content.length]; in.read(buff, 0, content.length); assertArrayEquals(content, buff); } } finally { tempFile.delete(); } } finally { inFile.delete(); } }
@Test public void testAppendFile() throws Exception { String fileName = "hello"; final byte[] content = "Hello".getBytes(); File tempFile = File.createTempFile("tarArchiverTest", ".tar"); try { try (TarArchiver instance = new TarArchiver(tempFile)) { Boolean result = instance.appendFile(fileName, new FilesArchiver.FileAppender<Boolean>() { @Override public Boolean append(OutputStream outputStream) throws IOException { outputStream.write(content); return true; } }); assertTrue(result); } try (TarArchiveInputStream in = new TarArchiveInputStream( new FileInputStream(tempFile))) { ArchiveEntry nextEntry = in.getNextEntry(); String name = nextEntry.getName(); assertEquals(fileName, name); byte[] buff = new byte[content.length]; in.read(buff, 0, content.length); assertArrayEquals(content, buff); } } finally { tempFile.delete(); } }