Example usage for java.util.zip GZIPOutputStream GZIPOutputStream

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

Introduction

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

Prototype

public GZIPOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates a new output stream with a default buffer size.

Usage

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;/*from  w w  w  .  java  2  s  . c o  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;/*www  .j  a va 2s.  c  o 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: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;/*  ww  w  . j a  va  2s .c o  m*/
    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[] args) throws Exception {

    InputStream fin = new FileInputStream("a.dat");
    OutputStream fout = new FileOutputStream("a.dat.gz");
    GZIPOutputStream gzout = new GZIPOutputStream(fout);
    for (int c = fin.read(); c != -1; c = fin.read()) {
        gzout.write(c);/*from  ww w .  ja  v  a2s  .c o  m*/
    }
    gzout.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    User admin = new User();
    admin.setId(new Long(1));
    User foo = new User();
    foo.setId(new Long(2));

    ObjectOutputStream oos = new ObjectOutputStream(
            new GZIPOutputStream(new FileOutputStream(new File("user.dat"))));
    oos.writeObject(admin);/*from w ww  .  j  av  a2s.  co m*/
    oos.writeObject(foo);
    oos.flush();
    oos.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;// w  ww .  j a v a 2  s  . co  m
    try {
        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:MainClass.java

public static void main(String[] args) {

    for (int i = 0; i < args.length; i++) {
        try {/*from   w w  w .  j  a v a2  s . co  m*/
            InputStream fin = new FileInputStream(args[i]);
            OutputStream fout = new FileOutputStream(args[i] + GZIP_SUFFIX);
            GZIPOutputStream gzout = new GZIPOutputStream(fout);
            for (int c = fin.read(); c != -1; c = fin.read()) {
                gzout.write(c);
            }
            gzout.close();
        } catch (IOException ex) {
            System.err.println(ex);
        }
    }
}

From source file:CompressIt.java

public static void main(String[] args) {
    String filename = args[0];//from  w ww  .j ava2s  . 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: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;//from w ww  .  j a v a2 s . c  om
    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();
}