Example usage for java.util.zip GZIPOutputStream write

List of usage examples for java.util.zip GZIPOutputStream write

Introduction

In this page you can find the example usage for java.util.zip GZIPOutputStream write.

Prototype

public synchronized void write(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Writes array of bytes to the compressed output stream.

Usage

From source file:cn.sharesdk.analysis.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;//from   ww  w  .j a v a  2  s .  co m
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);

    } catch (IOException e) {
        e.printStackTrace();
        Ln.e("NetworkHelper", "Base64Gzip == >>", e);
    }

    //Ln.i("after base64gizp", result);
    return result;
}

From source file:Main.java

public static String compressGzipFile(String filename) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "gzip.zip";

        try {// w  w  w  . j  ava 2 s .  c  o m
            FileInputStream fis = new FileInputStream(uncompressedFile);
            FileOutputStream fos = new FileOutputStream(compressedFile);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos) {
                {
                    def.setLevel(Deflater.BEST_COMPRESSION);
                }
            };

            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();
            return compressedFile;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;

}

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

public static File compressFile(File source) throws IOException {
    String tmpName = source.getName() + ".tmp";
    File tempFile = new File(source.getParentFile(), tmpName);
    FileUtils.copyFile(source, tempFile);

    String targetName = source.getName() + ".gz";
    File file = new File(source.getParentFile(), targetName);

    if (file.exists())
        file.delete();//from  w  w  w.  j  a va2  s .  co m
    file.createNewFile();

    byte[] buffer = new byte[1024];

    try {
        GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(file));
        FileInputStream in = new FileInputStream(tempFile);

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

        in.close();
        out.close();
        tempFile.delete();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    Bukkit.getLogger().info("Compressed file " + source.getName() + " to " + file.getName() + ".");
    return file;
}

From source file:cn.sharesdk.net.NetworkHelper.java

public static String Base64Gzip(String str) {
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    String result = null;/*from ww  w .  j  a  va2 s  .  co m*/
    // gzip
    GZIPOutputStream gos;
    try {
        gos = new GZIPOutputStream(baos);
        int count;
        byte data[] = new byte[1024];
        while ((count = bais.read(data, 0, 1024)) != -1) {
            gos.write(data, 0, count);
        }
        gos.finish();
        gos.close();

        byte[] output = baos.toByteArray();
        baos.flush();
        baos.close();
        bais.close();

        result = Base64.encodeToString(output, Base64.NO_WRAP);
    } catch (IOException e) {
        e.printStackTrace();
        Ln.i("NetworkHelper", "Base64Gzip == >>", e);
    }
    return result;
}

From source file:org.cloudata.core.common.io.CWritableUtils.java

public static int writeCompressedByteArray(DataOutput out, byte[] bytes) throws IOException {
    if (bytes != null) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzout = new GZIPOutputStream(bos);
        gzout.write(bytes, 0, bytes.length);
        gzout.close();//from w ww .  j  a va2 s.  c  om
        byte[] buffer = bos.toByteArray();
        int len = buffer.length;
        out.writeInt(len);
        out.write(buffer, 0, len);
        /* debug only! Once we have confidence, can lose this. */
        return ((bytes.length != 0) ? (100 * buffer.length) / bytes.length : 0);
    } else {
        out.writeInt(-1);
        return -1;
    }
}

From source file:org.apache.myfaces.shared_impl.util.StateUtils.java

public static byte[] compress(byte[] bytes) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {//from  w w w .j  ava  2s.  c o  m
        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        gzip.write(bytes, 0, bytes.length);
        gzip.finish();
        byte[] fewerBytes = baos.toByteArray();
        gzip.close();
        baos.close();
        gzip = null;
        baos = null;
        return fewerBytes;
    } catch (IOException e) {
        throw new FacesException(e);
    }
}

From source file:org.commoncrawl.util.shared.ArcFileReaderTests.java

/**
 * Gzip passed bytes. Use only when bytes is small.
 * /*from  www. ja v  a 2  s  .c o m*/
 * @param bytes
 *          What to gzip.
 * @return A gzip member of bytes.
 * @throws IOException
 */
static byte[] gzip(byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOS = new GZIPOutputStream(baos);
    gzipOS.write(bytes, 0, bytes.length);
    gzipOS.close();
    return baos.toByteArray();
}

From source file:org.wso2.carbon.apimgt.impl.GZIPUtils.java

public static void compressFile(String sourcePath, String destinationPath) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Compressing file : " + sourcePath + " to : " + destinationPath);
    }/*  w  w w.ja  va 2 s  .com*/
    byte[] buffer = new byte[BUFFER_SIZE];
    GZIPOutputStream gzipOutputStream = null;
    FileOutputStream fileOutputStream = null;
    FileInputStream fileInputStream = null;

    try {
        fileOutputStream = new FileOutputStream(destinationPath);
        gzipOutputStream = new GZIPOutputStream(fileOutputStream);
        fileInputStream = new FileInputStream(sourcePath);

        int length = 0;
        while ((length = fileInputStream.read(buffer)) > 0) {
            gzipOutputStream.write(buffer, 0, length);
        }
        gzipOutputStream.finish();
    } catch (IOException e) {
        throw new APIManagementException(
                "Error while compressing file at " + sourcePath + " to" + destinationPath, e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
        IOUtils.closeQuietly(fileOutputStream);
        IOUtils.closeQuietly(gzipOutputStream);
    }
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input file into a GZIP file container.
 *
 * @param anInPathFileName The file name to be compressed.
 * @param aGzipPathFileName The file name of the ZIP container.
 *
 * @throws IOException Related to opening the file streams and
 * related read/write operations./*from   ww w .j  a  va2 s  .c om*/
 */
public static void gzipFile(String anInPathFileName, String aGzipPathFileName) throws IOException {
    int byteCount;

    FileInputStream fileInputStream = new FileInputStream(anInPathFileName);
    FileOutputStream fileOutputStream = new FileOutputStream(aGzipPathFileName);
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(fileOutputStream);

    byte[] ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    byteCount = fileInputStream.read(ioBuf);
    while (byteCount > 0) {
        gzipOutputStream.write(ioBuf, 0, byteCount);
        byteCount = fileInputStream.read(ioBuf);
    }

    gzipOutputStream.close();
    fileOutputStream.close();
    fileInputStream.close();
}

From source file:Main.java

public static void compressFile(String inputFilePath, boolean deleteSource) throws IOException {
    byte[] buffer = new byte[1024];
    FileOutputStream fos = null;//from w  w  w.j  av a  2 s.  c om
    GZIPOutputStream gzos = null;
    FileInputStream fis = null;

    try {
        fos = new FileOutputStream(inputFilePath + ".gz");
        gzos = new GZIPOutputStream(fos);

        fis = new FileInputStream(inputFilePath);
        //         String zipEntryName = inputFilePath.substring(inputFilePath.lastIndexOf(File.separator) + 1);         
        //         gzos.putNextEntry(new ZipEntry(zipEntryName));

        int length;
        while ((length = fis.read(buffer)) > 0) {
            gzos.write(buffer, 0, length);
        }
    } finally {
        gzos.close();
        fos.close();
        fis.close();
    }

    if (deleteSource) {
        new File(inputFilePath).delete();
    }
}