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:CompressIt.java

public static void main(String[] args) {
    String filename = args[0];//from w w  w.j a v a2s . c o  m
    try {
        File file = new File(filename);
        int length = (int) file.length();
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = bis.read(buffer)) != -1) {
            gos.write(buffer, 0, bytesRead);
        }
        bis.close();
        gos.close();
        System.out.println("Input Length: " + length);
        System.out.println("Output Length: " + baos.size());
    } catch (FileNotFoundException e) {
        System.err.println("Invalid Filename");
    } catch (IOException e) {
        System.err.println("I/O Exception");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    int sChunk = 8192;

    String zipname = "a.gz";
    FileOutputStream out = new FileOutputStream(zipname);
    GZIPOutputStream zipout = new GZIPOutputStream(out);
    byte[] buffer = new byte[sChunk];

    FileInputStream in = new FileInputStream(args[0]);
    int length;//from   w w w.j a v  a  2  s  .  c  om
    while ((length = in.read(buffer, 0, sChunk)) != -1)
        zipout.write(buffer, 0, length);
    in.close();
    zipout.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream("outfile.gzip"));
    FileInputStream in = new FileInputStream("infilename");

    byte[] buf = new byte[1024];
    int len;/*  ww  w.j av  a  2  s .co  m*/
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();

    out.finish();
    out.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String outFilename = "outfile.gzip";
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(outFilename));

    String inFilename = "infilename";
    FileInputStream in = new FileInputStream(inFilename);

    byte[] buf = new byte[1024];
    int len;// w  ww.  j a  v  a 2 s  . co  m
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();

    out.finish();
    out.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;//from  w w  w.  ja v  a 2 s .c  o  m
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        line = bis.readLine();
        if (line == null)
            break;
        line = line + "\n";
        zip.write(line.getBytes(), 0, line.length());
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:CompRcv.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;//  w  ww. ja v a2 s . co  m
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        try {
            line = bis.readLine();
            if (line == null)
                break;
            line = line + "\n";
            zip.write(line.getBytes(), 0, line.length());
        } catch (Exception e) {
            break;
        }
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:MainClass.java

public static void main(String[] args) {

    int bufferSize = 8192;
    // create output stream
    String sourceFileName = "data.txt";
    String zipname = sourceFileName + ".gz";
    GZIPOutputStream zipout;
    try {/*w  ww.ja  v  a 2 s  .  c  o m*/
        FileOutputStream out = new FileOutputStream(zipname);
        zipout = new GZIPOutputStream(out);
    } catch (IOException e) {
        System.out.println("Couldn't create " + zipname + ".");
        return;
    }
    byte[] buffer = new byte[bufferSize];
    // compress the file
    try {
        FileInputStream in = new FileInputStream(sourceFileName);
        int length;
        while ((length = in.read(buffer, 0, bufferSize)) != -1)
            zipout.write(buffer, 0, length);
        in.close();
    } catch (IOException e) {
        System.out.println("Couldn't compress " + sourceFileName + ".");
    }
    try {
        zipout.close();
    } catch (IOException e) {
    }
}

From source file:Main.java

public static byte[] compressGZIP(byte bytes[]) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    GZIPOutputStream gzipos = new GZIPOutputStream(os);
    gzipos.write(bytes, 0, bytes.length);
    gzipos.close();/*w ww.j  a v a 2s  .  c  o  m*/
    return os.toByteArray();
}

From source file:Main.java

public static final byte[] compress(byte[] val) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
    GZIPOutputStream gos = new GZIPOutputStream(bos);
    gos.write(val, 0, val.length);
    gos.finish();// w  w  w.  java2s  .c o m
    gos.close();

    // store it and set compression flag
    return bos.toByteArray();
}

From source file:Main.java

public static byte[] compressInGzip(byte[] originalData, int offset, int length) throws Exception {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutStream = new GZIPOutputStream(bos);
    gzipOutStream.write(originalData, offset, length);
    gzipOutStream.finish();/*from  w w w . ja  v  a2s.c  o m*/
    gzipOutStream.flush();
    gzipOutStream.close();
    byte[] compressData = bos.toByteArray();
    bos.close();

    return compressData;
}