use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream in project mycore by MyCoRe-Org.
the class MCRTransferPackagePacker method buildTar.
/**
* Builds a *.tar archive at the path for the given transfer package.
*
* @param pathToTar where to store the *.tar
* @param transferPackage the package to zip
* @throws IOException something went wrong while packing
*/
private void buildTar(Path pathToTar, MCRTransferPackage transferPackage) throws IOException {
FileOutputStream fos = new FileOutputStream(pathToTar.toFile());
try (TarArchiveOutputStream tarOutStream = new TarArchiveOutputStream(fos)) {
for (Entry<String, MCRContent> contentEntry : transferPackage.getContent().entrySet()) {
String filePath = contentEntry.getKey();
byte[] data = contentEntry.getValue().asByteArray();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Adding '{}' to {}", filePath, pathToTar.toAbsolutePath());
}
writeFile(tarOutStream, filePath, data);
writeMD5(tarOutStream, filePath + ".md5", data);
}
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream 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_POSIX);
tarStream.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
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.archivers.tar.TarArchiveOutputStream 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.archivers.tar.TarArchiveOutputStream in project fabric-sdk-java by hyperledger.
the class LifecycleChaincodePackage method fromSource.
/**
* @param label Any name you like to identify this package.
* @param chaincodeSource Chaincode source directory.
* @param chaincodeType Chaincode type GO, JAVA.
* @param chaincodePath Only valid for GO LANG chaincode. Otherwise, null.
* @param chaincodeMetaInfLocation MetaInf location. Can be null.
* @return
* @throws IOException
*/
public static LifecycleChaincodePackage fromSource(String label, Path chaincodeSource, TransactionRequest.Type chaincodeType, String chaincodePath, Path chaincodeMetaInfLocation) throws IOException, InvalidArgumentException {
if (Utils.isNullOrEmpty(label)) {
throw new InvalidArgumentException("The parameter label may not be null or empty.");
}
if (null == chaincodeSource) {
throw new InvalidArgumentException(" The parameter chaincodeSource may not be null.");
}
if (null == chaincodeType) {
throw new InvalidArgumentException(" The parameter chaincodeType may not be null.");
}
byte[] mataDataBytes = generatePackageMataDataBytes(label, chaincodePath, chaincodeType);
byte[] dataBytes = generatePackageDataBytes(chaincodeSource, chaincodeMetaInfLocation, chaincodeType, chaincodePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream(500000);
// String sourcePath = sourceDirectory.getAbsolutePath();
TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bos));
archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
TarArchiveEntry archiveEntry = new TarArchiveEntry("metadata.json");
archiveEntry.setMode(0100644);
archiveEntry.setSize(mataDataBytes.length);
archiveOutputStream.putArchiveEntry(archiveEntry);
archiveOutputStream.write(mataDataBytes);
archiveOutputStream.closeArchiveEntry();
archiveEntry = new TarArchiveEntry("code.tar.gz");
archiveEntry.setMode(0100644);
archiveEntry.setSize(dataBytes.length);
archiveOutputStream.putArchiveEntry(archiveEntry);
archiveOutputStream.write(dataBytes);
archiveOutputStream.closeArchiveEntry();
archiveOutputStream.close();
return fromBytes(bos.toByteArray());
}
use of org.apache.commons.compress.archivers.tar.TarArchiveOutputStream 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();
try (TarArchiveOutputStream archiveOutputStream = new TarArchiveOutputStream(new GzipCompressorOutputStream(bos))) {
archiveOutputStream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
Collection<File> childrenFiles = org.apache.commons.io.FileUtils.listFiles(sourceDirectory, null, true);
ArchiveEntry archiveEntry;
for (File childFile : childrenFiles) {
String childPath = childFile.getAbsolutePath();
String relativePath = childPath.substring((sourcePath.length() + 1));
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);
archiveOutputStream.putArchiveEntry(archiveEntry);
try (FileInputStream fileInputStream = new FileInputStream(childFile)) {
IOUtils.copy(fileInputStream, archiveOutputStream);
} finally {
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);
archiveOutputStream.putArchiveEntry(archiveEntry);
try (FileInputStream fileInputStream = new FileInputStream(childFile)) {
IOUtils.copy(fileInputStream, archiveOutputStream);
} finally {
archiveOutputStream.closeArchiveEntry();
}
}
}
}
return bos.toByteArray();
}
Aggregations