Example usage for org.apache.commons.compress.compressors.gzip GzipUtils getCompressedFilename

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

Introduction

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

Prototype

public static String getCompressedFilename(String filename) 

Source Link

Document

Maps the given filename to the name that the file should have after compression with gzip.

Usage

From source file:cc.arduino.contributions.GZippedJsonDownloader.java

public void download(File tmpFile, Progress progress, String statusText, ProgressListener progressListener)
        throws Exception {
    try {/*w  w  w . ja  va 2 s.co  m*/
        File gzipTmpFile = new File(tmpFile.getParentFile(),
                GzipUtils.getCompressedFilename(tmpFile.getName()));
        // remove eventual leftovers from previous downloads
        if (gzipTmpFile.exists()) {
            gzipTmpFile.delete();
        }
        new JsonDownloader(downloader, gzippedUrl).download(gzipTmpFile, progress, statusText,
                progressListener);
        decompress(gzipTmpFile, tmpFile);
        gzipTmpFile.delete();
    } catch (Exception e) {
        new JsonDownloader(downloader, url).download(tmpFile, progress, statusText, progressListener);
    }
}

From source file:divconq.ctp.stream.GzipStream.java

@Override
public ReturnOption handle(FileDescriptor file, ByteBuf data) {
    if (file == FileDescriptor.FINAL)
        return this.downstream.handle(file, data);

    // we don't know what to do with a folder at this stage - gzip is for file content only
    // folder scanning is upstream in the FileSourceStream and partners
    if (file.isFolder())
        return ReturnOption.CONTINUE;

    // init if not set for this round of processing 
    if (this.deflater == null) {
        this.deflater = new Deflater(this.compressionLevel, true);
        this.crc.reset();
        this.writeHeader = true;
    }//from w ww .j  a v  a2 s. co m

    ByteBuf in = data;
    ByteBuf out = null;

    if (in != null) {
        byte[] inAry = in.array();

        // always allow for a header (10) plus footer (8) plus extra (12)
        // in addition to content
        int sizeEstimate = (int) Math.ceil(in.readableBytes() * 1.001) + 30;
        out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate);

        if (this.writeHeader) {
            this.writeHeader = false;
            out.writeBytes(gzipHeader);
        }

        this.crc.update(inAry, in.arrayOffset(), in.writerIndex());

        this.deflater.setInput(inAry, in.arrayOffset(), in.writerIndex());

        while (!this.deflater.needsInput())
            deflate(out);
    } else
        out = Hub.instance.getBufferAllocator().heapBuffer(30);

    FileDescriptor blk = new FileDescriptor();

    if (StringUtil.isEmpty(this.lastpath)) {
        if (StringUtil.isNotEmpty(this.nameHint))
            this.lastpath = "/" + this.nameHint;
        else if (file.getPath() != null)
            this.lastpath = "/" + GzipUtils.getCompressedFilename(file.path().getFileName());
        else
            this.lastpath = "/" + FileUtil.randomFilename("gz");
    }

    blk.setPath(this.lastpath);

    file.setModTime(System.currentTimeMillis());

    if (file.isEof()) {
        this.deflater.finish();

        while (!this.deflater.finished())
            deflate(out);

        int crcValue = (int) this.crc.getValue();

        out.writeByte(crcValue);
        out.writeByte(crcValue >>> 8);
        out.writeByte(crcValue >>> 16);
        out.writeByte(crcValue >>> 24);

        int uncBytes = this.deflater.getTotalIn();

        out.writeByte(uncBytes);
        out.writeByte(uncBytes >>> 8);
        out.writeByte(uncBytes >>> 16);
        out.writeByte(uncBytes >>> 24);

        this.deflater.end();
        this.deflater = null; // cause a reset for next time we use stream

        blk.setEof(true);
    }

    if (in != null)
        in.release();

    return this.downstream.handle(blk, out);
}

From source file:io.ecarf.core.utils.Utils.java

/**
 * Compress a file/*  w  ww.ja  v  a  2 s  . c  om*/
 * @param inFile
 * @param outFile
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String compressFile(String inFile) throws FileNotFoundException, IOException {

    byte[] buffer = new byte[BUFFER_SIZE];

    String outFile = GzipUtils.getCompressedFilename(inFile);

    log.info("Compressing file: " + inFile + ", to file: " + outFile);

    try (GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outFile), Constants.GZIP_BUF_SIZE);
            FileInputStream in = new FileInputStream(inFile);) {

        int len;
        while ((len = in.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }

        gzos.finish();
    }

    return outFile;
}