Example usage for java.util.zip CRC32 CRC32

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

Introduction

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

Prototype

public CRC32() 

Source Link

Document

Creates a new CRC32 object.

Usage

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*w  w w  . j  av a 2 s . co  m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:FileUtils.java

/**
 * Utility method for copying file/*from ww  w . j av  a  2  s .co  m*/
 * @param srcFile - source file
 * @param destFile - destination file
 */
public static void copyFile(File srcFile, File destFile) throws IOException {
    if (!srcFile.getPath().toLowerCase().endsWith(JPG) && !srcFile.getPath().toLowerCase().endsWith(JPEG)) {
        return;
    }
    final InputStream in = new FileInputStream(srcFile);
    final OutputStream out = new FileOutputStream(destFile);
    try {
        long millis = System.currentTimeMillis();
        CRC32 checksum;
        if (VERIFY) {
            checksum = new CRC32();
            checksum.reset();
        }
        final byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = in.read(buffer);
        while (bytesRead >= 0) {
            if (VERIFY) {
                checksum.update(buffer, 0, bytesRead);
            }
            out.write(buffer, 0, bytesRead);
            bytesRead = in.read(buffer);
        }
        if (CLOCK) {
            millis = System.currentTimeMillis() - millis;
            System.out.println("Copy file '" + srcFile.getPath() + "' on " + millis / 1000L + " second(s)");
        }
    } catch (IOException e) {
        throw e;
    } finally {
        out.close();
        in.close();
    }
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/*w w  w . j a  va 2 s .c  om*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setMethod(ZipEntry.DEFLATED);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);//from  w  ww . j ava 2 s.co m

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    entry.setTime(new Date().getTime());
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}

From source file:com.smash.revolance.ui.model.helper.ArchiveHelper.java

public static File buildArchive(File archive, File... files) throws FileNotFoundException {
    FileOutputStream fos = new FileOutputStream(archive);
    ZipOutputStream zos = new ZipOutputStream(fos);
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();

    for (File file : files) {
        if (!file.exists()) {
            System.err.println("Skipping: " + file);
            continue;
        }// w w  w.j a  v a2  s  .  co m
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            crc.reset();
            while ((bytesRead = bis.read(buffer)) != -1) {
                crc.update(buffer, 0, bytesRead);
            }

            bis.close();

            // Reset to beginning of input stream
            bis = new BufferedInputStream(new FileInputStream(file));
            String entryPath = FileHelper.getRelativePath(archive.getParentFile(), file);

            ZipEntry entry = new ZipEntry(entryPath);
            entry.setMethod(ZipEntry.STORED);
            entry.setCompressedSize(file.length());
            entry.setSize(file.length());
            entry.setCrc(crc.getValue());
            zos.putNextEntry(entry);
            while ((bytesRead = bis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesRead);
            }
        } catch (FileNotFoundException e) {

        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } finally {
            IOUtils.closeQuietly(bis);
        }
    }
    IOUtils.closeQuietly(zos);
    return archive;
}

From source file:kr.ac.cau.mecs.cass.signal.payload.JSONObjectPayload.java

@Override
public void serializeRawData(Payload payload) {
    String data = this.data.toString();

    Checksum crc32 = new CRC32();
    Base64 base64 = new Base64();

    byte[] bytedata = data.getBytes(Charset.forName("UTF-8"));

    crc32.reset();/*from   w  w w  .ja v  a2 s  .  c  o  m*/
    crc32.update(bytedata, 0, bytedata.length);

    String encoded = base64.encode(bytedata);

    payload.setCrc(crc32.getValue());
    payload.setLength(encoded.length());
    payload.setRawdata(encoded);

    payload.setType(0x11);
}

From source file:org.apache.tika.server.writer.ZipWriter.java

private static void zipStoreBuffer(ZipArchiveOutputStream zip, String name, byte[] dataBuffer)
        throws IOException {
    ZipEntry zipEntry = new ZipEntry(name != null ? name : UUID.randomUUID().toString());
    zipEntry.setMethod(ZipOutputStream.STORED);

    zipEntry.setSize(dataBuffer.length);
    CRC32 crc32 = new CRC32();
    crc32.update(dataBuffer);/*w ww  .ja va 2 s .  com*/
    zipEntry.setCrc(crc32.getValue());

    try {
        zip.putArchiveEntry(new ZipArchiveEntry(zipEntry));
    } catch (ZipException ex) {
        if (name != null) {
            zipStoreBuffer(zip, "x-" + name, dataBuffer);
            return;
        }
    }

    zip.write(dataBuffer);

    zip.closeArchiveEntry();
}

From source file:ch.cyberduck.core.io.CRC32ChecksumCompute.java

@Override
public Checksum compute(final InputStream in, final TransferStatus status) throws ChecksumException {
    final CRC32 crc32 = new CRC32();
    try {//  w w w.jav a2s  .c o  m
        byte[] buffer = new byte[16384];
        int bytesRead;
        while ((bytesRead = in.read(buffer, 0, buffer.length)) != -1) {
            crc32.update(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        throw new ChecksumException(LocaleFactory.localizedString("Checksum failure", "Error"), e.getMessage(),
                e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return new Checksum(HashAlgorithm.crc32, Long.toHexString(crc32.getValue()));
}

From source file:brut.util.BrutIO.java

public static CRC32 calculateCrc(InputStream input) throws IOException {
    CRC32 crc = new CRC32();
    int bytesRead;
    byte[] buffer = new byte[8192];
    while ((bytesRead = input.read(buffer)) != -1) {
        crc.update(buffer, 0, bytesRead);
    }//  ww  w.  j  ava2 s .  c  o  m
    return crc;
}

From source file:de.berlin.magun.nfcmime.core.CrcGenerator.java

/**
 * Generates a CRC32 checksum for an array of NdefRecords.
 * @param records/*from   w ww.  j a  v a2 s  . co m*/
 * @return CRC32 checksum represented as long.
 */
public long getChecksum(NdefRecord[] records) {
    byte[] overallBytes = getByteArray(records);
    Checksum cs = new CRC32();
    cs.update(overallBytes, 0, overallBytes.length);
    return cs.getValue();
}