Utils { public static byte[] generateTarGz(File sourceDirectory, String pathPrefix) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(500000); String sourcePath = sourceDirectory.getAbsolutePath(); TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bos)); archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); try { Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true); ArchiveEntry archiveEntry; FileInputStream fileInputStream; for (File childFile : childrenFiles) { String childPath = childFile.getAbsolutePath(); String relativePath = childPath.substring((sourcePath.length() + 1), childPath.length()); if (pathPrefix != null) { relativePath = Utils.combinePaths(pathPrefix, relativePath); } relativePath = FilenameUtils.separatorsToUnix(relativePath); archiveEntry = new TarArchiveEntry(childFile, relativePath); fileInputStream = new FileInputStream(childFile); archiveOutputStream.putArchiveEntry(archiveEntry); try { IOUtils.copy(fileInputStream, archiveOutputStream); } finally { IOUtils.closeQuietly(fileInputStream); archiveOutputStream.closeArchiveEntry(); } } } finally { IOUtils.closeQuietly(archiveOutputStream); } return bos.toByteArray(); } private  Utils(); static String generateParameterHash(String path, String func, List<String> args); static String generateDirectoryHash(String rootDir, String chaincodeDir, String hash); static byte[] generateTarGz(File sourceDirectory, String pathPrefix); static byte[] readFile(File input); static String generateUUID(); static Timestamp generateTimestamp(); static void deleteFileOrDirectory(File file); static byte[] hash(byte[] input, Digest digest); static String combinePaths(String first, String... other); static byte[] readFileFromClasspath(String fileName); static Properties parseGrpcUrl(String url); static Exception checkGrpcUrl(String url); static boolean isNullOrEmpty(String url); static String logString(final String string); static byte[] generateNonce(); static String toHexString(ByteString byteString); static String toHexString(byte[] bytes);  }
@Test public void testGenerateTarGz() throws Exception { File tempDir = createTempDirWithFiles(); byte[] data = Utils.generateTarGz(tempDir, "newPath"); Assert.assertNotNull(data); Assert.assertTrue(data.length > 0); }
@Test public void testGenerateTarGzEmptyDirectory() throws Exception { File emptyDir = tempFolder.newFolder("subfolder"); byte[] data = Utils.generateTarGz(emptyDir, null); Assert.assertNotNull(data); Assert.assertTrue(data.length > 0); }
@Test(expected = IllegalArgumentException.class) public void testGenerateTarGzNoDirectory() throws Exception { File rootDir = tempFolder.getRoot().getAbsoluteFile(); File nonExistentDir = new File(rootDir, "temp"); Utils.generateTarGz(nonExistentDir, null); Assert.fail("Expected an IOException as the directory does not exist"); }