use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project pinot by linkedin.
the class TarGzCompressionUtils method createTarGzOfDirectory.
public static String createTarGzOfDirectory(String directoryPath, String tarGzPath, String entryPrefix) throws IOException {
FileOutputStream fOut = null;
BufferedOutputStream bOut = null;
GzipCompressorOutputStream gzOut = null;
TarArchiveOutputStream tOut = null;
if (!tarGzPath.endsWith(TAR_GZ_FILE_EXTENTION)) {
tarGzPath = tarGzPath + TAR_GZ_FILE_EXTENTION;
}
try {
fOut = new FileOutputStream(new File(tarGzPath));
bOut = new BufferedOutputStream(fOut);
gzOut = new GzipCompressorOutputStream(bOut);
tOut = new TarArchiveOutputStream(gzOut);
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
addFileToTarGz(tOut, directoryPath, entryPrefix);
} catch (IOException e) {
LOGGER.error("Failed to create tar.gz file for {} at path: {}", directoryPath, tarGzPath, e);
Utils.rethrowException(e);
} finally {
if (tOut != null) {
tOut.finish();
tOut.close();
}
if (gzOut != null) {
gzOut.close();
}
if (bOut != null) {
bOut.close();
}
if (fOut != null) {
fOut.close();
}
}
return tarGzPath;
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project fabric-sdk-java by hyperledger.
the class Utils method generateTarGz.
/**
* Compress the contents of given directory using Tar and Gzip to an in-memory byte array.
*
* @param sourceDirectory the source directory.
* @param pathPrefix a path to be prepended to every file name in the .tar.gz output, or {@code null} if no prefix is required.
* @param chaincodeMetaInf
* @return the compressed directory contents.
* @throws IOException
*/
public static byte[] generateTarGz(File sourceDirectory, String pathPrefix, File chaincodeMetaInf) throws IOException {
logger.trace(format("generateTarGz: sourceDirectory: %s, pathPrefix: %s, chaincodeMetaInf: %s", sourceDirectory == null ? "null" : sourceDirectory.getAbsolutePath(), pathPrefix, chaincodeMetaInf == null ? "null" : chaincodeMetaInf.getAbsolutePath()));
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);
if (TRACE_ENABED) {
logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath()));
}
archiveEntry = new TarArchiveEntry(childFile, relativePath);
fileInputStream = new FileInputStream(childFile);
archiveOutputStream.putArchiveEntry(archiveEntry);
try {
IOUtils.copy(fileInputStream, archiveOutputStream);
} finally {
IOUtils.closeQuietly(fileInputStream);
archiveOutputStream.closeArchiveEntry();
}
}
if (null != chaincodeMetaInf) {
childrenFiles = org.apache.commons.io.FileUtils.listFiles(chaincodeMetaInf, null, true);
final URI metabase = chaincodeMetaInf.toURI();
for (File childFile : childrenFiles) {
final String relativePath = Paths.get("META-INF", metabase.relativize(childFile.toURI()).getPath()).toString();
if (TRACE_ENABED) {
logger.trace(format("generateTarGz: Adding '%s' entry from source '%s' to archive.", relativePath, childFile.getAbsolutePath()));
}
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();
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project fabric-sdk-java by hyperledger.
the class Util method generateTarGzInputStream.
/**
* Generate a targz inputstream from source folder.
*
* @param src Source location
* @param pathPrefix prefix to add to the all files found.
* @return return inputstream.
* @throws IOException
*/
public static InputStream generateTarGzInputStream(File src, String pathPrefix) throws IOException {
File sourceDirectory = src;
ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);
String sourcePath = sourceDirectory.getAbsolutePath();
TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(new BufferedOutputStream(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 new ByteArrayInputStream(bos.toByteArray());
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project tycho by eclipse.
the class TarGzArchiver method createArchive.
public void createArchive() throws IOException {
validate();
log.info("Building tar: " + destFile);
TarArchiveOutputStream tarStream = null;
try {
destFile.getAbsoluteFile().getParentFile().mkdirs();
GzipCompressorOutputStream gzipStream = new GzipCompressorOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));
tarStream = new TarArchiveOutputStream(gzipStream, "UTF-8");
// allow "long" file paths (> 100 chars)
tarStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
for (File sourceDir : sourceDirs) {
for (File child : sourceDir.listFiles()) {
addToTarRecursively(sourceDir, child, tarStream);
}
}
} finally {
if (tarStream != null) {
tarStream.close();
}
}
}
use of org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream in project neo4j by neo4j.
the class LoaderTest method shouldGiveAClearErrorMessageIfTheArchiveIsNotInTarFormat.
@Test
void shouldGiveAClearErrorMessageIfTheArchiveIsNotInTarFormat() throws IOException {
Path archive = testDirectory.file("the-archive.dump");
try (GzipCompressorOutputStream compressor = new GzipCompressorOutputStream(Files.newOutputStream(archive))) {
byte[] bytes = new byte[1000];
new Random().nextBytes(bytes);
compressor.write(bytes);
}
deleteLayoutFolders(databaseLayout);
IncorrectFormat incorrectFormat = assertThrows(IncorrectFormat.class, () -> new Loader().load(archive, databaseLayout));
assertEquals(archive.toString(), incorrectFormat.getMessage());
}
Aggregations