Example usage for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory

List of usage examples for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers ArchiveStreamFactory ArchiveStreamFactory.

Prototype

ArchiveStreamFactory

Source Link

Usage

From source file:net.sf.regain.crawler.preparator.ZipPreparator.java

/**
 * Prepares the document for indexing/*from  www .ja v  a2 s .c  o  m*/
 *
 * @param rawDocument the document
 * @throws RegainException if preparation goes wrong
 */
@Override
public void prepare(RawDocument rawDocument) throws RegainException {
    ArchiveInputStream ain = null;
    ZipInputStream zipInputStream = new ZipInputStream(rawDocument.getContentAsStream());

    PreparatorFactory preparatorFactory = new PreparatorFactory(new DummyCrawlerConfig());
    try {
        ain = new ArchiveStreamFactory().createArchiveInputStream("zip", rawDocument.getContentAsStream());
        ZipArchiveEntry entry;
        while ((entry = (ZipArchiveEntry) ain.getNextEntry()) != null) {
            String s = String.format("Entry: %s len %d added %TD", entry.getName(), entry.getSize(),
                    new Date(entry.getTime()));
            System.out.println(s);

            Preparator preparator = null;
            ByteArrayOutputStream byteArrayOutputStream = null;

            RawDocument rawZipDocument = new RawDocument(null, null, null, null);
            rawZipDocument.setUrl(new File(entry.getName()).toURI().toString());

            try {
                byteArrayOutputStream = new ByteArrayOutputStream();
                IOUtils.copy(zipInputStream, byteArrayOutputStream);
                rawZipDocument.setContent(byteArrayOutputStream.toByteArray());
                preparator = preparatorFactory.get(rawZipDocument);

            } finally {
                IOUtils.closeQuietly(byteArrayOutputStream);
            }

            if (preparator != null) {
                preparator.prepare(rawZipDocument);
                // concatenates contents
                setCleanedContent(new StringBuilder().append(getCleanedContent()).append("\n")
                        .append(preparator.getCleanedContent()).toString());
                setTitle(getTitle() + " " + preparator.getTitle());
                setSummary(getSummary() + " " + preparator.getSummary());
                setCleanedMetaData(getCleanedMetaData() + " " + preparator.getCleanedMetaData());
                setHeadlines(getHeadlines() + " " + preparator.getHeadlines());
                preparator.cleanUp();
            }
        }

    } catch (IOException | ArchiveException e) {
        e.printStackTrace();
    } finally {
        //IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(ain);
    }

}

From source file:com.endgame.binarypig.util.BuildSequenceFileFromArchive.java

public void load(FileSystem fs, Configuration conf, File archive, Path outputDir) throws Exception {
    Text key = new Text();
    BytesWritable val = new BytesWritable();

    SequenceFile.Writer writer = null;
    ArchiveInputStream archiveInputStream = null;

    try {/*from   w  w w .  j  ava  2s.c om*/
        Path sequenceName = new Path(outputDir, archive.getName() + ".seq");
        System.out.println("Writing to " + sequenceName);
        writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class, BytesWritable.class,
                CompressionType.RECORD);
        String lowerName = archive.toString().toLowerCase();

        if (lowerName.endsWith(".tar.gz") || lowerName.endsWith(".tgz")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new GZIPInputStream(new FileInputStream(archive)));
        } else if (lowerName.endsWith(".tar.bz") || lowerName.endsWith(".tar.bz2")
                || lowerName.endsWith(".tbz")) {
            FileInputStream is = new FileInputStream(archive);
            is.read(); // read 'B'
            is.read(); // read 'Z'
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new CBZip2InputStream(is));
        } else if (lowerName.endsWith(".tar")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("tar",
                    new FileInputStream(archive));
        } else if (lowerName.endsWith(".zip")) {
            archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream("zip",
                    new FileInputStream(archive));
        } else {
            throw new RuntimeException("Can't handle archive format for: " + archive);
        }

        ArchiveEntry entry = null;
        while ((entry = archiveInputStream.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                try {
                    byte[] outputFile = IOUtils.toByteArray(archiveInputStream);
                    val.set(outputFile, 0, outputFile.length);
                    key.set(DigestUtils.md5Hex(outputFile));

                    writer.append(key, val);
                } catch (IOException e) {
                    System.err.println("Warning: archive may be truncated: " + archive);
                    // Truncated Archive
                    break;
                }
            }
        }
    } finally {
        archiveInputStream.close();
        writer.close();
    }
}

From source file:com.github.benmanes.caffeine.cache.simulator.parser.TextTraceReader.java

/** Returns the input stream, decompressing if required. */
private InputStream readFile(String filePath) throws IOException {
    BufferedInputStream input = new BufferedInputStream(openFile(filePath));
    input.mark(100);// w  w  w.j  av  a2 s . c o m
    try {
        return new CompressorStreamFactory().createCompressorInputStream(input);
    } catch (CompressorException e) {
        input.reset();
    }
    try {
        return new ArchiveStreamFactory().createArchiveInputStream(input);
    } catch (ArchiveException e) {
        input.reset();
    }
    return input;
}

From source file:com.mediaworx.ziputils.Zipper.java

/**
 * Creates a new zip file.//www  .  j a  v a2 s.  c om
 * @param zipFilename           filename for the zip
 * @param zipTargetFolderPath   path of the target folder where the zip should be stored (it will be created if it
 *                              doesn't exist)
 * @throws IOException Exceptions from the underlying package framework are bubbled up
 */
public Zipper(String zipFilename, String zipTargetFolderPath) throws IOException {

    File targetFolder = new File(zipTargetFolderPath);
    if (!targetFolder.exists()) {
        FileUtils.forceMkdir(targetFolder);
    }

    if (!zipTargetFolderPath.endsWith(File.separator)) {
        zipTargetFolderPath = zipTargetFolderPath.concat(File.separator);
    }
    zipOutputStream = new FileOutputStream(zipTargetFolderPath + zipFilename);
    try {
        zip = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipOutputStream);
    } catch (ArchiveException e) {
        // This should never happen, because "zip" is a known archive type, but let's log it anyway
        LOG.error("Cant create an archive of type " + ArchiveStreamFactory.ZIP);
    }
}

From source file:ezbake.deployer.cli.commands.SSLCertsCommand.java

@Override
public void call() throws IOException, TException {
    String[] args = globalParameters.unparsedArgs;
    minExpectedArgs(2, args, this);
    String securityId = args[0];/*from   w w  w  . java 2 s . com*/
    String filePath = args[1];

    List<ArtifactDataEntry> certs = new ArrayList<>();
    EzSecurityRegistration.Client client = null;
    ThriftClientPool pool = poolSupplier.get();
    try {
        client = pool.getClient(EzSecurityRegistrationConstants.SERVICE_NAME,
                EzSecurityRegistration.Client.class);

        AppCerts s = client.getAppCerts(
                getSecurityToken(pool.getSecurityId(EzSecurityRegistrationConstants.SERVICE_NAME)), securityId);
        for (AppCerts._Fields fields : AppCerts._Fields.values()) {
            Object o = s.getFieldValue(fields);
            if (o instanceof byte[]) {
                String fieldName = fields.getFieldName().replace("_", ".");
                TarArchiveEntry tae = new TarArchiveEntry(
                        new File(new File(SSL_CONFIG_DIRECTORY, securityId), fieldName));
                certs.add(new ArtifactDataEntry(tae, (byte[]) o));
            }
        }

        ArchiveStreamFactory asf = new ArchiveStreamFactory();
        FileOutputStream fos = new FileOutputStream(filePath);
        GZIPOutputStream gzs = new GZIPOutputStream(fos);
        try (TarArchiveOutputStream aos = (TarArchiveOutputStream) asf
                .createArchiveOutputStream(ArchiveStreamFactory.TAR, gzs)) {
            aos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

            for (ArtifactDataEntry entry : certs) {
                aos.putArchiveEntry(entry.getEntry());
                IOUtils.write(entry.getData(), aos);
                aos.closeArchiveEntry();
            }
            aos.finish();
            gzs.finish();
        } catch (ArchiveException ex) {
            throw new DeploymentException(ex.getMessage());
        } finally {
            IOUtils.closeQuietly(fos);
        }
    } finally {
        pool.returnToPool(client);
    }
}

From source file:com.openkm.misc.ZipTest.java

public void testApache() throws IOException, ArchiveException {
    log.debug("testApache()");
    File zip = File.createTempFile("apache_", ".zip");

    // Create zip
    FileOutputStream fos = new FileOutputStream(zip);
    ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
    aos.putArchiveEntry(new ZipArchiveEntry("coeta"));
    aos.closeArchiveEntry();/*from   w  ww.ja va  2  s .  c  o  m*/
    aos.close();

    // Read zip
    FileInputStream fis = new FileInputStream(zip);
    ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
    ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
    assertEquals(zae.getName(), "coeta");
    ais.close();
}

From source file:net.orpiske.ssps.common.archive.TarArchiveUtils.java

/**
 * Unpacks a file/*from  w w  w. ja  v a2s.  c  o  m*/
 * @param source source file
 * @param destination destination directory. If the directory does not 
 * exists, it will be created
 * @param format archive format
 * @return the number of bytes processed
 * @throws SspsException
 * @throws ArchiveException
 * @throws IOException
 */
public static long unpack(File source, File destination, String format)
        throws SspsException, ArchiveException, IOException {

    if (!destination.exists()) {
        if (!destination.mkdirs()) {
            throw new IOException("Unable to create destination directory: " + destination.getPath());
        }
    } else {
        if (!destination.isDirectory()) {
            throw new SspsException(
                    "The provided destination " + destination.getPath() + " is not a directory");
        }
    }

    ArchiveStreamFactory factory = new ArchiveStreamFactory();

    FileInputStream inputFileStream = new FileInputStream(source);

    ArchiveInputStream archiveStream;
    try {
        archiveStream = factory.createArchiveInputStream(format, inputFileStream);
    } catch (ArchiveException e) {
        IOUtils.closeQuietly(inputFileStream);
        throw e;
    }

    OutputStream outStream = null;
    try {
        TarArchiveEntry entry = (TarArchiveEntry) archiveStream.getNextEntry();

        while (entry != null) {
            File outFile = new File(destination, entry.getName());

            if (entry.isDirectory()) {
                if (!outFile.exists()) {
                    if (!outFile.mkdirs()) {
                        throw new SspsException("Unable to create directory: " + outFile.getPath());
                    }
                } else {
                    logger.warn("Directory " + outFile.getPath() + " already exists. Ignoring ...");
                }
            } else {
                File parent = outFile.getParentFile();
                if (!parent.exists()) {
                    if (!parent.mkdirs()) {
                        throw new IOException("Unable to create parent directories " + parent.getPath());
                    }
                }

                outStream = new FileOutputStream(outFile);

                IOUtils.copy(archiveStream, outStream);
                outStream.close();
            }

            int mode = entry.getMode();

            PermissionsUtils.setPermissions(mode, outFile);

            entry = (TarArchiveEntry) archiveStream.getNextEntry();
        }

        inputFileStream.close();
        archiveStream.close();
    } finally {
        IOUtils.closeQuietly(outStream);
        IOUtils.closeQuietly(inputFileStream);
        IOUtils.closeQuietly(archiveStream);
    }

    return archiveStream.getBytesRead();
}

From source file:at.beris.virtualfile.provider.LocalArchiveOperationProvider.java

@Override
public List<File> extract(FileModel model, File target) throws IOException {
    List<File> fileList = new ArrayList<>();
    ArchiveInputStream ais = null;//from  w  ww . j  a  va  2 s .c  o  m
    InputStream fis = null;

    try {
        target.create();

        ArchiveStreamFactory archiveStreamFactory = new ArchiveStreamFactory();
        fis = new BufferedInputStream(new FileInputStream(new java.io.File(model.getUrl().toURI())));
        ais = archiveStreamFactory.createArchiveInputStream(fis);
        ArchiveEntry archiveEntry;

        while ((archiveEntry = ais.getNextEntry()) != null) {
            Map<String, URL> urlMap = getArchiveEntryURLMap(target.getUrl(), archiveEntry);

            if (archiveEntry.isDirectory()) {
                Files.createDirectory(new java.io.File(urlMap.get(URL).toURI()).toPath());
            } else {
                OutputStream out = new FileOutputStream(new java.io.File(urlMap.get(URL).toURI()));
                IOUtils.copy(ais, out);
                out.close();
            }

            File file = fileContext.newFile(urlMap.get(URL));
            fileList.add(file);
        }
    } catch (ArchiveException e) {
        throw new IOException(e);
    } catch (URISyntaxException e) {
        throw new IOException(e);
    } finally {
        if (ais != null)
            ais.close();
        if (fis != null)
            fis.close();
    }
    return fileList;
}

From source file:io.covert.binary.analysis.BuildSequenceFileFromTarball.java

public void load(FileSystem fs, Configuration conf, File inputTarball, Path outputDir) throws Exception {
    Text key = new Text();
    BytesWritable val = new BytesWritable();

    Path sequenceName = new Path(outputDir, inputTarball.getName() + ".seq");
    System.out.println("Writing to " + sequenceName);
    SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class,
            BytesWritable.class, CompressionType.RECORD);

    InputStream is = new FileInputStream(inputTarball);
    if (inputTarball.toString().toLowerCase().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    } else if (inputTarball.toString().toLowerCase().endsWith(".bz")
            || inputTarball.toString().endsWith(".bz2")) {
        is.read(); // read 'B'
        is.read(); // read 'Z'
        is = new CBZip2InputStream(is);
    }//w w w  .j  a  va2s  .c o  m

    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        if (!entry.isDirectory()) {

            try {
                final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream();
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
                byte[] outputFile = outputFileStream.toByteArray();
                val.set(outputFile, 0, outputFile.length);

                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(outputFile);
                byte[] digest = md.digest();
                String hexdigest = "";
                for (int i = 0; i < digest.length; i++) {
                    hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
                }
                key.set(hexdigest);
                writer.append(key, val);
            } catch (IOException e) {
                System.err.println("Warning: tarball may be truncated: " + inputTarball);
                // Truncated Tarball
                break;
            }
        }
    }
    debInputStream.close();
    writer.close();
}

From source file:com.surevine.gateway.scm.gatewayclient.GatewayPackage.java

void createTar(final Path tarPath, final Path... paths) throws IOException, ArchiveException {
    ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("tar",
            Files.newOutputStream(tarPath));
    try {/*w w w.  jav a2s  .c  o m*/
        for (Path path : paths) {
            TarArchiveEntry entry = new TarArchiveEntry(path.toFile());
            entry.setName(path.getFileName().toString());
            os.putArchiveEntry(entry);
            Files.copy(path, os);
            os.closeArchiveEntry();
        }
    } finally {
        os.close();
    }
}