Example usage for java.util.zip CheckedInputStream getChecksum

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

Introduction

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

Prototype

public Checksum getChecksum() 

Source Link

Document

Returns the Checksum for this input stream.

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) {
    }/* w w w.  j av a 2s . c o  m*/
    long checksum = cis.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 w w  .  j  a va 2 s  .  c  o  m*/
        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;// w  w w  .j  ava 2  s  .  c  o  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 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  a  va2  s .co 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:AbstractFileSystemHelper.java

public static long getChecksum(InputStream is) {
    CheckedInputStream cis = null;
    long checksum = 0;
    try {//from ww  w  .j  a v  a2s  .  co  m
        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  ww  w .j av a2  s.  c o  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:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file//w ww. j  a  v a2  s .  c  o m
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(File file, boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    int length = (int) file.length();
    byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamUtils.closeInputStream(cis);
    return checksum;
}

From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file//from w w  w.  ja v  a2s.com
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    final int length = (int) file.length();
    final byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamExtensions.closeInputStream(cis);
    return checksum;
}

From source file:de.alpharogroup.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file/* www  . ja  va 2  s .co m*/
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    final int length = (int) file.length();
    final byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamUtils.closeInputStream(cis);
    return checksum;
}

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/*from w  w w  . ja  v a 2 s.c  o  m*/
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

        // Computer CRC32 checksum
        cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        }

        checksum = cis.getChecksum().getValue();

    } catch (Exception e) {
    } finally {
        if (file != null)
            try {
                file.close();
            } catch (Exception e) {
            }
        ;
    }
    return checksum;
}