Example usage for org.apache.commons.compress.compressors.gzip GzipParameters GzipParameters

List of usage examples for org.apache.commons.compress.compressors.gzip GzipParameters GzipParameters

Introduction

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

Prototype

GzipParameters

Source Link

Usage

From source file:examples.RdfSerializationExample.java

public static void main(String[] args) throws IOException {

    // Define where log messages go
    ExampleHelpers.configureLogging();/*from   w w  w. j a v a 2s  . c o  m*/

    // Print information about this program
    printDocumentation();

    // Initialize sites; only needed to link to Wikipedia pages in RDF
    DumpProcessingController dumpProcessingController = new DumpProcessingController("wikidatawiki");
    dumpProcessingController.setOfflineMode(ExampleHelpers.OFFLINE_MODE);
    Sites sites = dumpProcessingController.getSitesInformation();

    // Prepare a compressed output stream to write the data to
    // (admittedly, this is slightly over-optimized for an example)
    OutputStream bufferedFileOutputStream = new BufferedOutputStream(
            ExampleHelpers.openExampleFileOuputStream("wikidata-simple-statements.nt.gz"), 1024 * 1024 * 5);
    GzipParameters gzipParameters = new GzipParameters();
    gzipParameters.setCompressionLevel(7);
    OutputStream compressorOutputStream = new GzipCompressorOutputStream(bufferedFileOutputStream,
            gzipParameters);
    OutputStream exportOutputStream = asynchronousOutputStream(compressorOutputStream);

    // Create a serializer processor
    RdfSerializer serializer = new RdfSerializer(RDFFormat.NTRIPLES, exportOutputStream, sites,
            PropertyRegister.getWikidataPropertyRegister());
    // Serialize simple statements (and nothing else) for all items
    serializer.setTasks(RdfSerializer.TASK_ITEMS | RdfSerializer.TASK_SIMPLE_STATEMENTS);

    // Run serialization
    serializer.open();
    ExampleHelpers.processEntitiesFromWikidataDump(serializer);
    serializer.close();
}

From source file:com.chenshu.compress.CompressOldTest.java

@Benchmark
public int commonsGzipCompress() {
    ByteArrayOutputStream bout = null;
    GzipCompressorOutputStream gzout = null;
    try {/*www. j  ava2 s .co  m*/
        GzipParameters p = new GzipParameters();
        p.setCompressionLevel(level);
        bout = new ByteArrayOutputStream(data.length);
        gzout = new GzipCompressorOutputStream(bout, p);
        gzout.write(data);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gzout != null) {
            try {
                gzout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bout != null) {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    byte[] bs = bout.toByteArray();
    return bs.length;
}

From source file:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

private static void writeZipFile(File directoryToZip, List<File> fileList, File target) {
    try {//from  w ww .j  a va  2 s .  co  m
        FileOutputStream fos = new FileOutputStream(target);
        GzipParameters parameters = new GzipParameters();
        parameters.setCompressionLevel(9);
        GzipCompressorOutputStream gzip = new GzipCompressorOutputStream(fos, parameters);
        TarArchiveOutputStream stream = new TarArchiveOutputStream(gzip);

        for (File file : fileList) {
            if (!file.isDirectory()) { // we only zip files, not directories
                int retryCount = 0;
                while (retryCount < 10) {
                    try {
                        addToZip(directoryToZip, file, stream);
                        break;
                    } catch (Exception e) {
                        retryCount++;
                        if (retryCount > 9)
                            e.printStackTrace();
                    }
                }
            }
        }

        stream.close();
        gzip.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.ant.compress.taskdefs.GZip.java

public GZip() {
    super(new PackBase.ResourceWrapper() {
        public CommonsCompressCompressorResource wrap(Resource dest) {
            return new GZipResource(dest);
        }/*  w w w.  jav  a 2 s . com*/
    });
    setFactory(new GZipStreamFactory() {
        public CompressorOutputStream getCompressorStream(OutputStream stream) throws IOException {
            GzipParameters params = new GzipParameters();
            params.setCompressionLevel(level);
            return new GzipCompressorOutputStream(stream, params);
        }
    });
}

From source file:org.wikidata.wdtk.client.DumpProcessingOutputAction.java

/**
 * Creates an compressing {@link OutputStream}. The result is owned by the
 * caller and should be closed later. Neverhteless, the {@link #close()}
 * method of this class must also be called, since it may free additional
 * resources created.//from  ww w . ja v  a 2 s  . com
 * 
 * @param useStdOut
 *            if true, {@link System#out} is returned and the other
 *            parameters are ignored
 * @param filePath
 *            the string name of the output file, possibly including path
 *            information
 * @param compressionType
 *            a string that refers to a type of output compression or the
 *            empty string (no compression); a suitable file extension will
 *            be added to the output file
 * 
 * @return compressing {@link OutputStream}
 * @throws IOException
 *             if there were problems opening the required streams
 */
protected OutputStream getOutputStream(boolean useStdOut, String filePath, String compressionType)
        throws IOException {
    if (useStdOut) {
        return System.out;
    }

    if (!compressionType.isEmpty()) {
        filePath += "." + compressionType;
    }

    Path outputDirectory = Paths.get(filePath).getParent();
    if (outputDirectory == null) {
        outputDirectory = Paths.get(".");
    }

    DirectoryManager dm = DirectoryManagerFactory.createDirectoryManager(outputDirectory);
    OutputStream out = dm.getOutputStreamForFile(Paths.get(filePath).getFileName().toString());

    OutputStream bufferedFileOutputStream = new BufferedOutputStream(out, 1024 * 1024 * 5);

    switch (compressionType) {
    case COMPRESS_BZ2:
        return getAsynchronousOutputStream(new BZip2CompressorOutputStream(bufferedFileOutputStream));
    case COMPRESS_GZIP:
        GzipParameters gzipParameters = new GzipParameters();
        gzipParameters.setCompressionLevel(7);
        return getAsynchronousOutputStream(
                new GzipCompressorOutputStream(bufferedFileOutputStream, gzipParameters));
    case COMPRESS_NONE:
        return bufferedFileOutputStream;
    default:
        bufferedFileOutputStream.close();
        throw new IllegalArgumentException("Unsupported compression format: " + compressionType);
    }
}

From source file:org.wikidata.wdtk.examples.RdfSerializationExample.java

/**
 * Creates a new RDF Serializer. Output is written to the file of the given
 * name (it will always be a compressed file, so the name should reflect
 * that). The tasks define what the serializer will be writing into this
 * file. The new serializer is also registered in an internal list, so it
 * can be started and closed more conveniently.
 *
 * @param outputFileName/* ww w  .j a  va  2s.  c o  m*/
 *            filename to write output to
 * @param compressionExtension
 *            the extension of the chosen compression format or the empty
 *            string for no compression
 * @param tasks
 *            an integer that is a bitwise OR of flags like
 *            {@link RdfSerializer#TASK_LABELS}.
 * @return the newly created serializer
 * @throws FileNotFoundException
 *             if the given file cannot be opened for writing for some
 *             reason
 * @throws IOException
 *             if it was not possible to write the BZ2 header to the file
 */
@SuppressWarnings("resource")
private static RdfSerializer createRdfSerializer(String outputFileName, String compressionExtension, int tasks)
        throws FileNotFoundException, IOException {

    OutputStream bufferedFileOutputStream = new BufferedOutputStream(
            ExampleHelpers.openExampleFileOuputStream(outputFileName + compressionExtension), 1024 * 1024 * 5);

    OutputStream compressorOutputStream = null;
    switch (compressionExtension) {
    case COMPRESS_BZ2:
        compressorOutputStream = new BZip2CompressorOutputStream(bufferedFileOutputStream);
        break;
    case COMPRESS_GZIP:
        GzipParameters gzipParameters = new GzipParameters();
        gzipParameters.setCompressionLevel(7);
        compressorOutputStream = new GzipCompressorOutputStream(bufferedFileOutputStream, gzipParameters);
        break;
    case COMPRESS_NONE:
        compressorOutputStream = bufferedFileOutputStream;
        break;
    default:
        throw new IllegalArgumentException("Unsupported compression format: " + compressionExtension);
    }

    OutputStream exportOutputStream = asynchronousOutputStream(compressorOutputStream);
    // // Alternative code: if not using separate threads, increase
    // // pre-compression buffer:
    // OutputStream exportOutputStream = new
    // BufferedOutputStream(compressorOutputStream,1024 * 1024 * 50);

    RdfSerializer serializer = new RdfSerializer(RDFFormat.NTRIPLES, exportOutputStream, sites);
    serializer.setTasks(tasks);

    dumpProcessingController.registerEntityDocumentProcessor(serializer, MwRevision.MODEL_WIKIBASE_ITEM, true);
    dumpProcessingController.registerEntityDocumentProcessor(serializer, MwRevision.MODEL_WIKIBASE_PROPERTY,
            true);

    serializers.add(serializer);
    serializerNames.add(outputFileName);

    return serializer;
}