Search in sources :

Example 96 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project quickutil by quickutil.

the class CompressUtil method decompressTarGz.

/**
 * 解压tar.gz文件:
 *
 * @return 解压后的根目录路径
 */
public static String decompressTarGz(String sourcePath, String targetPath) {
    String rootPath = null;
    try (FileInputStream fInput = new FileInputStream(sourcePath);
        BufferedInputStream bufInput = new BufferedInputStream(fInput);
        GZIPInputStream gzipInput = new GZIPInputStream(bufInput);
        ArchiveInputStream archiveInput = new ArchiveStreamFactory().createArchiveInputStream("tar", gzipInput)) {
        // tar压缩文件条目
        TarArchiveEntry entry;
        boolean isRootPath = true;
        while ((entry = (TarArchiveEntry) archiveInput.getNextEntry()) != null) {
            String entryName = entry.getName();
            // 转换为目标路径
            if (targetPath != null) {
                entryName = targetPath + File.separator + entryName;
            }
            if (isRootPath) {
                rootPath = entryName;
                isRootPath = false;
            }
            if (entry.isDirectory()) {
                FileUtil.mkdirByFile(entryName);
            } else if (entry.isFile()) {
                FileUtil.stream2file(archiveInput, entryName, false);
            }
        }
    } catch (Exception e) {
        LOGGER.error(Symbol.BLANK, e);
    }
    return rootPath;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 97 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project docker-maven-plugin by fabric8io.

the class ImageArchiveUtilTest method readInvalidJsonInArchive.

@Test
public void readInvalidJsonInArchive() throws IOException {
    byte[] archiveBytes;
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(baos)) {
        final byte[] entryData = ("}" + UUID.randomUUID().toString() + "{").getBytes();
        TarArchiveEntry tarEntry = new TarArchiveEntry("not-the-" + ImageArchiveUtil.MANIFEST_JSON);
        tarEntry.setSize(entryData.length);
        tarOutput.putArchiveEntry(tarEntry);
        tarOutput.write(entryData);
        tarOutput.closeArchiveEntry();
        tarOutput.finish();
        archiveBytes = baos.toByteArray();
    }
    ImageArchiveManifest manifest = ImageArchiveUtil.readManifest(new ByteArrayInputStream(archiveBytes));
    Assert.assertNull(manifest);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ImageArchiveManifest(io.fabric8.maven.docker.model.ImageArchiveManifest) Test(org.junit.Test)

Example 98 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project karaf by apache.

the class ArchiveMojo method addFileToTarGz.

private void addFileToTarGz(TarArchiveOutputStream tOut, Path f, String base) throws IOException {
    if (Files.isDirectory(f)) {
        String entryName = base + f.getFileName().toString() + "/";
        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tOut.putArchiveEntry(tarEntry);
        tOut.closeArchiveEntry();
        try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
            for (Path child : children) {
                addFileToTarGz(tOut, child, entryName);
            }
        }
    } else if (useSymLinks && Files.isSymbolicLink(f)) {
        String entryName = base + f.getFileName().toString();
        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName, TarConstants.LF_SYMLINK);
        tarEntry.setLinkName(Files.readSymbolicLink(f).toString());
        tOut.putArchiveEntry(tarEntry);
        tOut.closeArchiveEntry();
    } else {
        String entryName = base + f.getFileName().toString();
        TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
        tarEntry.setSize(Files.size(f));
        if (entryName.contains("/bin/") || (!usePathPrefix && entryName.startsWith("bin/"))) {
            if (entryName.endsWith(".bat")) {
                tarEntry.setMode(0644);
            } else {
                tarEntry.setMode(0755);
            }
        }
        tOut.putArchiveEntry(tarEntry);
        Files.copy(f, tOut);
        tOut.closeArchiveEntry();
    }
}
Also used : Path(java.nio.file.Path) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 99 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project twister2 by DSC-SPIDAL.

the class TarGzipPacker method addZipToArchive.

/**
 * given tar.gz file will be copied to this tar.gz file.
 * all files will be transferred to new tar.gz file one by one.
 * original directory structure will be kept intact
 *
 * @param zipFile the archive file to be copied to the new archive
 * @param dirPrefixForTar sub path inside the archive
 */
public boolean addZipToArchive(String zipFile, String dirPrefixForTar) {
    try {
        // construct input stream
        ZipFile zipFileObj = new ZipFile(zipFile);
        Enumeration<? extends ZipEntry> entries = zipFileObj.entries();
        // copy the existing entries from source gzip file
        while (entries.hasMoreElements()) {
            ZipEntry nextEntry = entries.nextElement();
            TarArchiveEntry entry = new TarArchiveEntry(dirPrefixForTar + nextEntry.getName());
            entry.setSize(nextEntry.getSize());
            entry.setModTime(nextEntry.getTime());
            tarOutputStream.putArchiveEntry(entry);
            IOUtils.copy(zipFileObj.getInputStream(nextEntry), tarOutputStream);
            tarOutputStream.closeArchiveEntry();
        }
        zipFileObj.close();
        return true;
    } catch (IOException ioe) {
        LOG.log(Level.SEVERE, "Archive File can not be added: " + zipFile, ioe);
        return false;
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 100 with TarArchiveEntry

use of org.apache.commons.compress.archivers.tar.TarArchiveEntry in project twister2 by DSC-SPIDAL.

the class TarGzipPacker method addFileToArchive.

/**
 * add one file to tar.gz file
 *
 * @param file file to be added to the tar.gz
 */
public boolean addFileToArchive(File file, String dirPrefixForTar) {
    try {
        String filePathInTar = dirPrefixForTar + file.getName();
        TarArchiveEntry entry = new TarArchiveEntry(file, filePathInTar);
        entry.setSize(file.length());
        if (!file.isDirectory() && file.canExecute()) {
            entry.setMode(0755);
        }
        tarOutputStream.putArchiveEntry(entry);
        IOUtils.copy(new FileInputStream(file), tarOutputStream);
        tarOutputStream.closeArchiveEntry();
        return true;
    } catch (IOException e) {
        LOG.log(Level.SEVERE, "File can not be added: " + file.getName(), e);
        return false;
    }
}
Also used : IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) FileInputStream(java.io.FileInputStream)

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