Example usage for java.util.zip CheckedInputStream CheckedInputStream

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

Introduction

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

Prototype

public CheckedInputStream(InputStream in, Checksum cksum) 

Source Link

Document

Creates an input stream using the specified Checksum.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CheckedInputStream cis = new CheckedInputStream(new FileInputStream("filename"), new Adler32());
    byte[] tempBuf = new byte[128];
    while (cis.read(tempBuf) >= 0) {
    }//from   w w  w . j  ava2  s  . co m
    long checksum = cis.getChecksum().getValue();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "d.zip";
    CheckedInputStream checksum = new CheckedInputStream(new FileInputStream(zipname), new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;/*w w  w . j ava  2 s  .c o  m*/

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());
        int size;
        byte[] buffer = new byte[2048];

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entry.getName()),
                buffer.length);
        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }

    zis.close();
    System.out.println("Checksum = " + checksum.getChecksum().getValue());
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");

    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;//w ww  .  ja v  a  2  s . c om
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();

    System.out.println("Checksum: " + csum.getChecksum().getValue());

    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();

}

From source file:ZipCompress.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    // No corresponding getComment(), though.
    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;/*from ww w .j av a  2  s.  co  m*/
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();
    // Checksum valid only after the file has been closed!
    System.out.println("Checksum: " + csum.getChecksum().getValue());
    // Now extract the files:
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    // Alternative way to open and read zip files:
    ZipFile zf = new ZipFile("test.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze2 = (ZipEntry) e.nextElement();
        System.out.println("File: " + ze2);
        // ... and extract the data as before
    }
}

From source file:Main.java

public static String getCRC32(File file) {
    CRC32 crc32 = new CRC32();
    CheckedInputStream checkedinputstream = null;
    String crc = null;/*from w ww.j  a  v a  2 s .co m*/
    try {
        checkedinputstream = new CheckedInputStream(new FileInputStream(file), crc32);
        byte[] buf = new byte[1024];
        while (checkedinputstream.read(buf) >= 0) {
        }
        crc = Long.toHexString(crc32.getValue()).toUpperCase();
    } catch (Exception e) {
        Log.w(TAG, e);
        Log.e("WxException", e.getMessage(), e);
    } finally {
        if (checkedinputstream != null) {
            try {
                checkedinputstream.close();
            } catch (IOException e) {
            }
        }
    }
    return crc;
}

From source file:MultiFileChecksumHelper.java

public static long getChecksum(File[] files) {
    CheckedInputStream cis = null;
    FileInputStream is = null;/*from   w  w  w.j ava2 s  . c o m*/
    Checksum checksum = new Adler32();
    byte[] tempBuf = new byte[128];

    for (int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++) {
        try {
            is = new FileInputStream(files[i]);
            cis = new CheckedInputStream(is, checksum);
            while (cis.read(tempBuf) >= 0) {
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (cis != null) {
                try {
                    cis.close();
                } catch (IOException ioe) {
                }
                cis = null;
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                }
                is = null;
            }
        }
    }
    return checksum.getValue();
}

From source file:AbstractFileSystemHelper.java

public static long getChecksum(InputStream is) {
    CheckedInputStream cis = null;
    long checksum = 0;
    try {//from w w  w.ja  v  a  2  s .  c  om
        cis = new CheckedInputStream(is, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        checksum = cis.getChecksum().getValue();
    } catch (IOException e) {
        checksum = 0;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
            }
        }
    }
    return checksum;
}

From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java

public static long getCRC32(File file) throws IOException {
    CheckedInputStream cis = null;
    long fileSize = 0;
    // Computer CRC32 checksum
    cis = new CheckedInputStream(new FileInputStream(file), new CRC32());

    fileSize = file.length();//from   w w  w  . j  a  va  2 s  . co  m

    byte[] buf = new byte[128];
    while (cis.read(buf) != -1)
        ;

    long checksum = cis.getChecksum().getValue();
    cis.close();
    Logger.verbose("CRC32 of %s is %d", file.getPath(), checksum);
    return checksum;
}

From source file:com.petercho.Encoder.java

private static Checksum checksum(InputStream is, Checksum checksum) throws IOException {
    InputStream in = new CheckedInputStream(is, checksum);
    IOUtils.copy(in, new NullOutputStream());
    return checksum;
}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void _unzipArchive(File archive, File outputDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(archive);
    CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;//from www  .  j  av a  2 s .  c o  m
    while ((entry = zis.getNextEntry()) != null) {
        log.debug("Extracting: " + entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(entry.getName());
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    log.debug("Checksum:" + checksum.getChecksum().getValue());

}