Search in sources :

Example 36 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project packr by libgdx.

the class ArchiveUtilsTest method testArchiveDuplicateEntry.

/**
 * Adds the same entry to a Zip file to ensure that extraction handles duplicates properly.
 */
@Test
public void testArchiveDuplicateEntry(@TempDir Path tempDir) throws IOException, ArchiveException, CompressorException {
    String someFilename = "some-file.txt";
    Path someFilePath = tempDir.resolve(someFilename);
    Files.write(someFilePath, "Hello world\n".getBytes(StandardCharsets.UTF_8));
    Path archiveZip = tempDir.resolve("archive.zip");
    // Create an archive, add entry, update file, add same entry
    try (OutputStream fileOutputStream = new BufferedOutputStream(Files.newOutputStream(archiveZip));
        ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(ZIP.getCommonsCompressName(), fileOutputStream)) {
        // Create an entry for some file
        ArchiveEntry entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename);
        archiveOutputStream.putArchiveEntry(entry);
        Files.copy(someFilePath, archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
        // Update some file, and put it into the archive again
        Files.write(someFilePath, "Good bye\n".getBytes(StandardCharsets.UTF_8));
        entry = archiveOutputStream.createArchiveEntry(someFilePath.toFile(), someFilename);
        archiveOutputStream.putArchiveEntry(entry);
        Files.copy(someFilePath, archiveOutputStream);
        archiveOutputStream.closeArchiveEntry();
        archiveOutputStream.finish();
    }
    Path extractionDirectory = tempDir.resolve("extract");
    Files.createDirectories(extractionDirectory);
    ArchiveUtils.extractArchive(archiveZip, extractionDirectory);
    assertEquals(new String(Files.readAllBytes(tempDir.resolve(someFilename)), StandardCharsets.UTF_8), new String(Files.readAllBytes(extractionDirectory.resolve(someFilename)), StandardCharsets.UTF_8), "Extracted file contents should have matched original");
}
Also used : Path(java.nio.file.Path) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) BufferedOutputStream(java.io.BufferedOutputStream) ArchiveOutputStream(org.apache.commons.compress.archivers.ArchiveOutputStream) Test(org.junit.jupiter.api.Test)

Example 37 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project camel by apache.

the class TarAggregationStrategy method addEntryToTar.

private void addEntryToTar(File source, String entryName, byte[] buffer, int length) throws IOException, ArchiveException {
    File tmpTar = File.createTempFile(source.getName(), null, parentDir);
    tmpTar.delete();
    if (!source.renameTo(tmpTar)) {
        throw new IOException("Cannot create temp file: " + source.getName());
    }
    FileInputStream fis = new FileInputStream(tmpTar);
    TarArchiveInputStream tin = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    TarArchiveOutputStream tos = new TarArchiveOutputStream(new FileOutputStream(source));
    tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
    tos.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
    // copy the existing entries    
    ArchiveEntry nextEntry;
    while ((nextEntry = tin.getNextEntry()) != null) {
        tos.putArchiveEntry(nextEntry);
        IOUtils.copy(tin, tos);
        tos.closeArchiveEntry();
    }
    // Create new entry
    TarArchiveEntry entry = new TarArchiveEntry(entryName);
    entry.setSize(length);
    tos.putArchiveEntry(entry);
    tos.write(buffer, 0, length);
    tos.closeArchiveEntry();
    IOHelper.close(fis, tin, tos);
    LOG.trace("Deleting temporary file: {}", tmpTar);
    FileUtil.deleteFile(tmpTar);
}
Also used : TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) ArchiveStreamFactory(org.apache.commons.compress.archivers.ArchiveStreamFactory) FileOutputStream(java.io.FileOutputStream) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IOException(java.io.IOException) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) GenericFile(org.apache.camel.component.file.GenericFile) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) FileInputStream(java.io.FileInputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry)

Example 38 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project neo4j by neo4j.

the class Dumper method withEntry.

private void withEntry(ThrowingAction<IOException> operation, Path root, ArchiveOutputStream stream, Path file) throws IOException {
    ArchiveEntry entry = createEntry(file, root, stream);
    stream.putArchiveEntry(entry);
    operation.apply();
    stream.closeArchiveEntry();
}
Also used : ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 39 with ArchiveEntry

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

the class RunMojo method extract.

private static void extract(ArchiveInputStream is, File targetDir) throws IOException {
    try {
        if (targetDir.exists()) {
            FileUtils.forceDelete(targetDir);
        }
        targetDir.mkdirs();
        ArchiveEntry entry = is.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            name = name.substring(name.indexOf("/") + 1);
            File file = new File(targetDir, name);
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                file.getParentFile().mkdirs();
                OutputStream os = new FileOutputStream(file);
                try {
                    IOUtils.copy(is, os);
                } finally {
                    IOUtils.closeQuietly(os);
                }
            }
            entry = is.getNextEntry();
        }
    } finally {
        is.close();
    }
}
Also used : ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry)

Example 40 with ArchiveEntry

use of org.apache.commons.compress.archivers.ArchiveEntry in project tika by apache.

the class CXFTestBase method readArchiveFromStream.

protected Map<String, String> readArchiveFromStream(ArchiveInputStream zip) throws IOException {
    Map<String, String> data = new HashMap<String, String>();
    while (true) {
        ArchiveEntry entry = zip.getNextEntry();
        if (entry == null) {
            break;
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        IOUtils.copy(zip, bos);
        data.put(entry.getName(), DigestUtils.md5Hex(bos.toByteArray()));
    }
    return data;
}
Also used : HashMap(java.util.HashMap) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ArchiveEntry(org.apache.commons.compress.archivers.ArchiveEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

ArchiveEntry (org.apache.commons.compress.archivers.ArchiveEntry)62 File (java.io.File)24 FileInputStream (java.io.FileInputStream)24 IOException (java.io.IOException)22 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)20 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)19 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)17 InputStream (java.io.InputStream)16 FileOutputStream (java.io.FileOutputStream)11 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)11 BufferedInputStream (java.io.BufferedInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 Path (java.nio.file.Path)9 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)9 ArchiveStreamFactory (org.apache.commons.compress.archivers.ArchiveStreamFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)6 OutputStream (java.io.OutputStream)5 TarArchiveOutputStream (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream)5 BufferedOutputStream (java.io.BufferedOutputStream)4