use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project mycore by MyCoRe-Org.
the class MCRTransferPackagePacker method writeFile.
/**
* Writes a file to a *.tar archive.
*
* @param tarOutStream the stream of the *.tar.
* @param fileName the file name to write
* @param data of the file
*
* @throws IOException some writing to the stream went wrong
*/
private void writeFile(TarArchiveOutputStream tarOutStream, String fileName, byte[] data) throws IOException {
TarArchiveEntry tarEntry = new TarArchiveEntry(fileName);
tarEntry.setSize(data.length);
tarOutStream.putArchiveEntry(tarEntry);
tarOutStream.write(data);
tarOutStream.closeArchiveEntry();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project mycore by MyCoRe-Org.
the class MCRTarServlet method sendMetadataCompressed.
@Override
protected void sendMetadataCompressed(String fileName, byte[] content, long lastModified, TarArchiveOutputStream container) throws IOException {
TarArchiveEntry entry = new TarArchiveEntry(fileName);
entry.setModTime(lastModified);
entry.setSize(content.length);
container.putArchiveEntry(entry);
container.write(content);
container.closeArchiveEntry();
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project gerrit by GerritCodeReview.
the class GetArchiveIT method getTarContent.
private HashMap<String, String> getTarContent(InputStream in) throws Exception {
HashMap<String, String> archiveEntries = new HashMap<>();
int bufferSize = 100;
try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) {
TarArchiveEntry entry;
while ((entry = tarIn.getNextTarEntry()) != null) {
if (entry.isDirectory()) {
archiveEntries.put(entry.getName(), null);
} else {
byte[] data = new byte[bufferSize];
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedOutputStream bufferedOut = new BufferedOutputStream(out, bufferSize)) {
int count;
while ((count = tarIn.read(data, 0, bufferSize)) != -1) {
bufferedOut.write(data, 0, count);
}
bufferedOut.flush();
archiveEntries.put(entry.getName(), out.toString());
}
}
}
}
return archiveEntries;
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project ats-framework by Axway.
the class LocalFileSystemOperations method extractTarGZip.
private void extractTarGZip(String tarGzipFilePath, String outputDirPath) {
TarArchiveEntry entry = null;
try (TarArchiveInputStream tis = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarGzipFilePath)))) {
while ((entry = (TarArchiveEntry) tis.getNextEntry()) != null) {
if (log.isDebugEnabled()) {
log.debug("Extracting " + entry.getName());
}
File entryDestination = new File(outputDirPath, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
OutputStream out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(tis, out, false, true);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
// check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(entry.getMode()));
}
}
} catch (Exception e) {
String errorMsg = null;
if (entry != null) {
errorMsg = "Unable to gunzip " + entry.getName() + " from " + tarGzipFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
} else {
errorMsg = "Could not read data from " + tarGzipFilePath;
}
throw new FileSystemOperationException(errorMsg, e);
}
}
use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project ats-framework by Axway.
the class LocalFileSystemOperations method extractTar.
private void extractTar(String tarFilePath, String outputDirPath) {
TarArchiveEntry entry = null;
try (TarArchiveInputStream tis = new TarArchiveInputStream(new FileInputStream(tarFilePath))) {
while ((entry = (TarArchiveEntry) tis.getNextEntry()) != null) {
if (log.isDebugEnabled()) {
log.debug("Extracting " + entry.getName());
}
File entryDestination = new File(outputDirPath, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
OutputStream out = new BufferedOutputStream(new FileOutputStream(entryDestination));
IoUtils.copyStream(tis, out, false, true);
}
if (OperatingSystemType.getCurrentOsType() != OperatingSystemType.WINDOWS) {
// check if the OS is UNIX
// set file/dir permissions, after it is created
Files.setPosixFilePermissions(entryDestination.getCanonicalFile().toPath(), getPosixFilePermission(entry.getMode()));
}
}
} catch (Exception e) {
String errorMsg = null;
if (entry != null) {
errorMsg = "Unable to untar " + StringEscapeUtils.escapeJava(entry.getName()) + " from " + tarFilePath + ".Target directory '" + outputDirPath + "' is in inconsistent state.";
} else {
errorMsg = "Could not read data from " + tarFilePath;
}
throw new FileSystemOperationException(errorMsg, e);
}
}
Aggregations