Example usage for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream

List of usage examples for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.xz XZCompressorInputStream XZCompressorInputStream.

Prototype

public XZCompressorInputStream(final InputStream inputStream) throws IOException 

Source Link

Document

Creates a new input stream that decompresses XZ-compressed data from the specified input stream.

Usage

From source file:de.l3s.streamcorpus.terrier.ThriftFileCollectionRecordReader.java

/** Open the next file in the collections 
 * @throws IOException */// w w  w .ja v  a2s  .co  m
private boolean getNextFile() throws IOException {

    if (paths == null || paths.isEmpty()) {
        return false;
    }

    // close the current file
    close();

    // no more paths to process
    if (++collectionIndex >= paths.size()) {
        return false;
    }

    Path path = new Path(paths.get(collectionIndex));
    // Some files are corrupted, report them and move on
    try {
        fis = fs.open(path);
        bis = new BufferedInputStream(fis);

        if (paths.get(collectionIndex).endsWith(".xz")) {
            xzis = new XZCompressorInputStream(bis);
            cis = new CountingInputStream(xzis);
        } else {
            xzis = null;
            cis = new CountingInputStream(bis);
        }
        transport = new TIOStreamTransport(cis);
        position = start = cis.getPos();
        length = fs.getFileStatus(path).getLen();

    } catch (IOException e) {
        LOG.error("Bad file: ", path.toString());
        e.printStackTrace();
    }

    try {
        if (transport != null)
            transport.open();

        // Skip this file
        else {
            return getNextFile();
        }
    } catch (TTransportException e) {
        e.printStackTrace();
        throw new IOException(e);
    }

    factory = new TBinaryProtocol.Factory();
    tp = factory.getProtocol(transport);
    value = new StreamItemWritable(factory);

    return true;
}

From source file:it.evilsocket.dsploit.core.UpdateService.java

/**
 * open a compressed InputStream/*from w  ww .j a  v a  2s. com*/
 * @param in the InputStream to decompress
 * @return the InputStream to read from
 * @throws IOException if an I/O error occurs
 */
private InputStream openCompressedStream(InputStream in) throws IOException {
    if (mCurrentTask.compression == null)
        return in;
    switch (mCurrentTask.compression) {
    default:
    case none:
        return in;
    case gzip:
        return new GzipCompressorInputStream(in);
    case bzip:
        return new BZip2CompressorInputStream(in);
    case xz:
        return new XZCompressorInputStream(in);
    }
}

From source file:com.anthemengineering.mojo.infer.InferMojo.java

/**
 * Extracts a given infer.tar.xz file to the given directory.
 *
 * @param tarXzToExtract the file to extract
 * @param inferDownloadDir the directory to extract the file to
 *//* w  w  w.  j a  va 2s.  c  o  m*/
private static void extract(File tarXzToExtract, File inferDownloadDir) throws IOException {

    FileInputStream fin = null;
    BufferedInputStream in = null;
    XZCompressorInputStream xzIn = null;
    TarArchiveInputStream tarIn = null;

    try {
        fin = new FileInputStream(tarXzToExtract);
        in = new BufferedInputStream(fin);
        xzIn = new XZCompressorInputStream(in);
        tarIn = new TarArchiveInputStream(xzIn);

        TarArchiveEntry entry;
        while ((entry = tarIn.getNextTarEntry()) != null) {
            final File fileToWrite = new File(inferDownloadDir, entry.getName());

            if (entry.isDirectory()) {
                FileUtils.forceMkdir(fileToWrite);
            } else {
                BufferedOutputStream out = null;
                try {
                    out = new BufferedOutputStream(new FileOutputStream(fileToWrite));
                    final byte[] buffer = new byte[4096];
                    int n = 0;
                    while (-1 != (n = tarIn.read(buffer))) {
                        out.write(buffer, 0, n);
                    }
                } finally {
                    if (out != null) {
                        out.close();
                    }
                }
            }

            // assign file permissions
            final int mode = entry.getMode();

            fileToWrite.setReadable((mode & 0004) != 0, false);
            fileToWrite.setReadable((mode & 0400) != 0, true);
            fileToWrite.setWritable((mode & 0002) != 0, false);
            fileToWrite.setWritable((mode & 0200) != 0, true);
            fileToWrite.setExecutable((mode & 0001) != 0, false);
            fileToWrite.setExecutable((mode & 0100) != 0, true);
        }
    } finally {
        if (tarIn != null) {
            tarIn.close();
        }
    }
}

From source file:net.staticsnow.nexus.repository.apt.internal.hosted.AptHostedFacet.java

@Transactional(retryOn = { ONeedRetryException.class })
public void ingestAsset(Payload body) throws IOException, PGPException {
    AptFacet aptFacet = getRepository().facet(AptFacet.class);
    StorageTx tx = UnitOfWork.currentTx();
    Bucket bucket = tx.findBucket(getRepository());

    ControlFile control = null;/*w ww. j a v  a  2s .  com*/
    try (TempStreamSupplier supplier = new TempStreamSupplier(body.openInputStream());
            ArArchiveInputStream is = new ArArchiveInputStream(supplier.get())) {
        ArchiveEntry debEntry;
        while ((debEntry = is.getNextEntry()) != null) {
            InputStream controlStream;
            switch (debEntry.getName()) {
            case "control.tar":
                controlStream = new CloseShieldInputStream(is);
                break;
            case "control.tar.gz":
                controlStream = new GZIPInputStream(new CloseShieldInputStream(is));
                break;
            case "control.tar.xz":
                controlStream = new XZCompressorInputStream(new CloseShieldInputStream(is));
            default:
                continue;
            }

            try (TarArchiveInputStream controlTarStream = new TarArchiveInputStream(controlStream)) {
                ArchiveEntry tarEntry;
                while ((tarEntry = controlTarStream.getNextEntry()) != null) {
                    if (tarEntry.getName().equals("control") || tarEntry.getName().equals("./control")) {
                        control = new ControlFileParser().parseControlFile(controlTarStream);
                    }
                }
            }
        }

        if (control == null) {
            throw new IllegalOperationException("Invalid Debian package supplied");
        }

        String name = control.getField("Package").map(f -> f.value).get();
        String version = control.getField("Version").map(f -> f.value).get();
        String architecture = control.getField("Architecture").map(f -> f.value).get();

        String assetName = name + "_" + version + "_" + architecture + ".deb";
        String assetPath = "pool/" + name.substring(0, 1) + "/" + name + "/" + assetName;

        Content content = aptFacet.put(assetPath,
                new StreamPayload(() -> supplier.get(), body.getSize(), body.getContentType()));
        Asset asset = Content.findAsset(tx, bucket, content);
        String indexSection = buildIndexSection(control, asset.size(),
                asset.getChecksums(FacetHelper.hashAlgorithms), assetPath);
        asset.formatAttributes().set(P_ARCHITECTURE, architecture);
        asset.formatAttributes().set(P_PACKAGE_NAME, name);
        asset.formatAttributes().set(P_PACKAGE_VERSION, version);
        asset.formatAttributes().set(P_INDEX_SECTION, indexSection);
        asset.formatAttributes().set(P_ASSET_KIND, "DEB");
        tx.saveAsset(asset);

        List<AssetChange> changes = new ArrayList<>();
        changes.add(new AssetChange(AssetAction.ADDED, asset));

        for (Asset removed : selectOldPackagesToRemove(name, architecture)) {
            tx.deleteAsset(removed);
            changes.add(new AssetChange(AssetAction.REMOVED, removed));
        }

        rebuildIndexesInTransaction(tx, changes.stream().toArray(AssetChange[]::new));
    }
}

From source file:org.apache.avro.file.XZCodec.java

@Override
public ByteBuffer decompress(ByteBuffer data) throws IOException {
    ByteArrayOutputStream baos = getOutputBuffer(data.remaining());
    InputStream bytesIn = new ByteArrayInputStream(data.array(), data.arrayOffset() + data.position(),
            data.remaining());//from  ww  w.  ja  va2  s  .  c o  m
    InputStream ios = new XZCompressorInputStream(bytesIn);
    try {
        IOUtils.copy(ios, baos);
    } finally {
        ios.close();
    }
    return ByteBuffer.wrap(baos.toByteArray());
}

From source file:org.apache.camel.impl.XZDataFormat.java

@Override
public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
    XZCompressorInputStream unzipInput = null;

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {//www.j  a v a  2s.  c  o m
        unzipInput = new XZCompressorInputStream(is);
        IOHelper.copy(unzipInput, bos);
        return bos.toByteArray();
    } finally {
        IOHelper.close(unzipInput, is);
    }
}

From source file:org.apereo.portal.io.xml.JaxbPortalDataHandlerService.java

protected void importDataArchive(Resource archive, InputStream resourceStream, BatchImportOptions options) {
    BufferedInputStream bufferedResourceStream = null;
    try {/* www. j  av  a  2  s .co m*/
        //Make sure the stream is buffered
        if (resourceStream instanceof BufferedInputStream) {
            bufferedResourceStream = (BufferedInputStream) resourceStream;
        } else {
            bufferedResourceStream = new BufferedInputStream(resourceStream);
        }

        //Buffer up to 100MB, bad things will happen if we bust this buffer.
        //TODO see if there is a buffered stream that will write to a file once the buffer fills up
        bufferedResourceStream.mark(100 * 1024 * 1024);
        final MediaType type = getMediaType(bufferedResourceStream, archive.getFilename());

        if (MT_JAVA_ARCHIVE.equals(type)) {
            final ArchiveInputStream archiveStream = new JarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MediaType.APPLICATION_ZIP.equals(type)) {
            final ArchiveInputStream archiveStream = new ZipArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_CPIO.equals(type)) {
            final ArchiveInputStream archiveStream = new CpioArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_AR.equals(type)) {
            final ArchiveInputStream archiveStream = new ArArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_TAR.equals(type)) {
            final ArchiveInputStream archiveStream = new TarArchiveInputStream(bufferedResourceStream);
            importDataArchive(archive, archiveStream, options);
        } else if (MT_BZIP2.equals(type)) {
            final CompressorInputStream compressedStream = new BZip2CompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_GZIP.equals(type)) {
            final CompressorInputStream compressedStream = new GzipCompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_PACK200.equals(type)) {
            final CompressorInputStream compressedStream = new Pack200CompressorInputStream(
                    bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else if (MT_XZ.equals(type)) {
            final CompressorInputStream compressedStream = new XZCompressorInputStream(bufferedResourceStream);
            importDataArchive(archive, compressedStream, options);
        } else {
            throw new RuntimeException("Unrecognized archive media type: " + type);
        }
    } catch (IOException e) {
        throw new RuntimeException("Could not load InputStream for resource: " + archive, e);
    } finally {
        IOUtils.closeQuietly(bufferedResourceStream);
    }
}

From source file:org.codehaus.plexus.archiver.xz.XZUnArchiver.java

public static @Nonnull XZCompressorInputStream getXZInputStream(InputStream in) throws ArchiverException {
    try {/*from   w  w  w. j  a v a2  s .  c  om*/
        return new XZCompressorInputStream(in);
    } catch (IOException ioe) {
        throw new ArchiverException("Trouble creating BZIP2 compressor, invalid file ?", ioe);
    }
}

From source file:org.crosswire.common.compress.XZ.java

public ByteArrayOutputStream uncompress(int expectedLength) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(expectedLength);
    CompressorInputStream in = new XZCompressorInputStream(input);
    IOUtils.copy(in, out);//from w  w  w .jav  a 2  s . c  o  m
    in.close();
    out.flush();
    out.close();
    return out;
}

From source file:org.eclipse.cdt.internal.p2.touchpoint.natives.actions.UnpackAction.java

@Override
public IStatus execute(Map<String, Object> parameters) {
    try {//from  w w w.  j  a va  2 s  . c  o m
        String source = (String) parameters.get(PARM_SOURCE);
        if (source == null) {
            return Activator.getStatus(IStatus.ERROR,
                    String.format(Messages.UnpackAction_ParmNotPresent, PARM_SOURCE, ACTION_NAME));
        }

        String targetDir = (String) parameters.get(PARM_TARGET_DIR);
        if (targetDir == null) {
            return Activator.getStatus(IStatus.ERROR,
                    String.format(Messages.UnpackAction_ParmNotPresent, PARM_TARGET_DIR, ACTION_NAME));
        }

        String format = (String) parameters.get(PARM_FORMAT);
        if (format == null) {
            return Activator.getStatus(IStatus.ERROR,
                    String.format(Messages.UnpackAction_ParmNotPresent, PARM_FORMAT, ACTION_NAME));
        }

        IProfile profile = (IProfile) parameters.get("profile"); //$NON-NLS-1$
        File installFolder = new File(profile.getProperty(IProfile.PROP_INSTALL_FOLDER));
        File destDir = new File(installFolder, targetDir);
        if (destDir.exists()) {
            return Activator.getStatus(IStatus.ERROR, String.format(
                    org.eclipse.cdt.internal.p2.touchpoint.natives.actions.Messages.UnpackAction_TargetDirExists,
                    destDir.getAbsolutePath()));
        }

        URL url = new URL(source);
        InputStream fileIn = new BufferedInputStream(url.openStream());

        switch (format) {
        case "tar.gz": //$NON-NLS-1$
            InputStream gzIn = new GzipCompressorInputStream(fileIn);
            untar(gzIn, destDir);
            break;
        case "tar.bz2": //$NON-NLS-1$
            InputStream bzIn = new BZip2CompressorInputStream(fileIn);
            untar(bzIn, destDir);
            break;
        case "tar.xz": //$NON-NLS-1$
            InputStream xzIn = new XZCompressorInputStream(fileIn);
            untar(xzIn, destDir);
            break;
        case "zip": //$NON-NLS-1$

        }

        return Status.OK_STATUS;
    } catch (Throwable e) {
        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
    }
}