Search in sources :

Example 86 with TarArchiveEntry

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();
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 87 with TarArchiveEntry

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();
}
Also used : TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 88 with TarArchiveEntry

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;
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 89 with TarArchiveEntry

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);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Example 90 with TarArchiveEntry

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);
    }
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) AttributeNotSupportedException(com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException)

Aggregations

TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)213 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)102 File (java.io.File)91 FileInputStream (java.io.FileInputStream)59 IOException (java.io.IOException)59 FileOutputStream (java.io.FileOutputStream)46 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)40 InputStream (java.io.InputStream)32 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)32 BufferedInputStream (java.io.BufferedInputStream)31 ByteArrayInputStream (java.io.ByteArrayInputStream)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)24 Test (org.junit.Test)24 Path (java.nio.file.Path)21 BufferedOutputStream (java.io.BufferedOutputStream)20 OutputStream (java.io.OutputStream)18 ArrayList (java.util.ArrayList)18 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)16 HashMap (java.util.HashMap)12 GZIPInputStream (java.util.zip.GZIPInputStream)12