Example usage for java.util.zip CRC32 getValue

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

Introduction

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

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

From source file:org.kuali.rice.krad.datadictionary.URLMonitor.java

private Long getCRC(URL zipUrl) {
    Long result = -1l;// w w w .  j av  a2 s  .c o  m
    try {
        CRC32 crc = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);

        byte[] buffer = new byte[1024];
        int length;

        //read the entry from zip file and extract it to disk
        while ((length = cis.read(buffer)) > 0)
            ;

        cis.close();

        result = crc.getValue();
    } catch (IOException e) {
        LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
    }
    return result;
}

From source file:org.apache.hadoop.raid.TestDirectoryRaidEncoder.java

private void printFileCRC(FileSystem fs, Path file, long bufferSize) throws IOException {
    byte[] buffer = new byte[(int) bufferSize];
    FSDataInputStream stm = fs.open(file);
    StringBuilder sb = new StringBuilder();
    sb.append("CRC for file: " + file + " size " + fs.getFileStatus(file).getLen() + "\n");
    while (stm.read(buffer) >= 0) {
        CRC32 crc = new CRC32();
        crc.update(buffer);// w w  w.  j  a  v a 2 s  .  com
        sb.append(" " + crc.getValue());
    }
    sb.append("\n");
    System.out.println(sb.toString());
    stm.close();
}

From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java

private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files)
        throws IOException {
    String UNK_DIRNAME = getUNK_DIRNAME();
    File unknownFileDir = new File(appDir, UNK_DIRNAME);

    // loop through unknown files
    for (Map.Entry<String, String> unknownFileInfo : files.entrySet()) {
        File inputFile = new File(unknownFileDir, unknownFileInfo.getKey());
        if (inputFile.isDirectory()) {
            continue;
        }/*  w ww  .ja  va 2s  . com*/

        ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
        int method = Integer.valueOf(unknownFileInfo.getValue());
        LogHelper.fine(
                String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method));
        if (method == ZipEntry.STORED) {
            newEntry.setMethod(ZipEntry.STORED);
            newEntry.setSize(inputFile.length());
            newEntry.setCompressedSize(-1);
            BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
            CRC32 crc = calculateCrc(unknownFile);
            newEntry.setCrc(crc.getValue());

            //                LogHelper.getLogger().fine("\tsize: " + newEntry.getSize());
        } else {
            newEntry.setMethod(ZipEntry.DEFLATED);
        }
        outputFile.putNextEntry(newEntry);

        BrutIO.copy(inputFile, outputFile);
        outputFile.closeEntry();
    }
}

From source file:org.apache.hadoop.raid.TestRaidShell.java

private long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);/*from w w w . j a v  a2  s. com*/
    for (int b = 0; b > 0; b = stm.read()) {
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:net.librec.util.FileUtil.java

/**
 * Zip a given folder/*from ww  w.  j  a va  2 s  .  co m*/
 *
 * @param dirPath    a given folder: must be all files (not sub-folders)
 * @param filePath   zipped file
 * @throws Exception if error occurs
 */
public static void zipFolder(String dirPath, String filePath) throws Exception {
    File outFile = new File(filePath);
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outFile));
    int bytesRead;
    byte[] buffer = new byte[1024];
    CRC32 crc = new CRC32();
    for (File file : listFiles(dirPath)) {
        BufferedInputStream 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));
        ZipEntry entry = new ZipEntry(file.getName());
        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);
        }
        bis.close();
    }
    zos.close();

    LOG.debug("A zip-file is created to: " + outFile.getPath());
}

From source file:org.apache.hadoop.dfs.TestDFSUpgradeFromImage.java

private void verifyDir(DFSClient client, String dir) throws IOException {

    DFSFileInfo[] fileArr = client.listPaths(dir);
    TreeMap<String, Boolean> fileMap = new TreeMap<String, Boolean>();

    for (DFSFileInfo file : fileArr) {
        String path = file.getPath().toString();
        fileMap.put(path, Boolean.valueOf(file.isDir()));
    }//  ww w.  j a  v  a  2s  .co m

    for (Iterator<String> it = fileMap.keySet().iterator(); it.hasNext();) {
        String path = it.next();
        boolean isDir = fileMap.get(path);

        overallChecksum.update(path.getBytes());

        if (isDir) {
            verifyDir(client, path);
        } else {
            // this is not a directory. Checksum the file data.
            CRC32 fileCRC = new CRC32();
            FSInputStream in = client.open(path);
            byte[] buf = new byte[4096];
            int nRead = 0;
            while ((nRead = in.read(buf, 0, buf.length)) > 0) {
                fileCRC.update(buf, 0, nRead);
            }

            verifyChecksum(path, fileCRC.getValue());
        }
    }
}

From source file:org.apache.hadoop.hdfs.TestRaidDfs.java

public static boolean validateFile(FileSystem fileSys, Path name, long length, long crc) throws IOException {

    long numRead = 0;
    CRC32 newcrc = new CRC32();
    FSDataInputStream stm = fileSys.open(name);
    final byte[] b = new byte[4192];
    int num = 0;//w  w w.  ja  va  2s  .  c  om
    while (num >= 0) {
        num = stm.read(b);
        if (num < 0) {
            break;
        }
        numRead += num;
        newcrc.update(b, 0, num);
    }
    stm.close();

    if (numRead != length) {
        LOG.info("Number of bytes read " + numRead + " does not match file size " + length);
        return false;
    }

    LOG.info(" Newcrc " + newcrc.getValue() + " old crc " + crc);
    if (newcrc.getValue() != crc) {
        LOG.info("CRC mismatch of file " + name + ": " + newcrc.getValue() + " vs. " + crc);
        return false;
    }
    return true;
}

From source file:org.apache.hadoop.hdfs.TestDFSUpgradeFromImage.java

private void verifyDir(DistributedFileSystem dfs, Path dir) throws IOException {

    FileStatus[] fileArr = dfs.listStatus(dir);
    TreeMap<Path, Boolean> fileMap = new TreeMap<Path, Boolean>();

    for (FileStatus file : fileArr) {
        fileMap.put(file.getPath(), Boolean.valueOf(file.isDir()));
    }//from w  w w .  j  a  v  a 2  s . c o m

    for (Iterator<Path> it = fileMap.keySet().iterator(); it.hasNext();) {
        Path path = it.next();
        boolean isDir = fileMap.get(path);

        String pathName = path.toUri().getPath();
        overallChecksum.update(pathName.getBytes());

        if (isDir) {
            verifyDir(dfs, path);
        } else {
            // this is not a directory. Checksum the file data.
            CRC32 fileCRC = new CRC32();
            FSInputStream in = dfs.dfs.open(pathName);
            byte[] buf = new byte[4096];
            int nRead = 0;
            while ((nRead = in.read(buf, 0, buf.length)) > 0) {
                fileCRC.update(buf, 0, nRead);
            }

            verifyChecksum(pathName, fileCRC.getValue());
        }
    }
}

From source file:org.sleuthkit.autopsy.experimental.configuration.SharedConfiguration.java

/**
 * Calculate the CRC of a file to use to determine if it has changed.
 *
 * @param filePath File to get the CRC for
 *
 * @return String containing the CRC//  w w  w.  j av  a 2  s. c o m
 *
 * @throws SharedConfigurationException
 */
private static String calculateCRC(String filePath) throws SharedConfigurationException {
    File file = new File(filePath);
    try {
        FileInputStream fileStream = new FileInputStream(file);
        CRC32 crc = new CRC32();
        byte[] buffer = new byte[65536];
        int bytesRead = fileStream.read(buffer);
        while (-1 != bytesRead) {
            crc.update(buffer, 0, bytesRead);
            bytesRead = fileStream.read(buffer);
        }
        return String.valueOf(crc.getValue());
    } catch (IOException ex) {
        throw new SharedConfigurationException(
                String.format("Failed to calculate CRC for %s", file.getAbsolutePath()), ex);
    }
}

From source file:org.docx4j.openpackaging.io3.stores.ZipPartStore.java

public void saveBinaryPart(Part part) throws Docx4JException {

    // Drop the leading '/'
    String resolvedPartUri = part.getPartName().getName().substring(1);

    try {//w w w  .  ja  va2s . c o m

        byte[] bytes = null;

        if (((BinaryPart) part).isLoaded()) {

            bytes = ((BinaryPart) part).getBytes();

        } else {

            if (this.sourcePartStore == null) {

                throw new Docx4JException("part store has changed, and sourcePartStore not set");

            } else if (this.sourcePartStore == this) {

                // Just use the ByteArray
                log.debug(part.getPartName() + " is clean");
                ByteArray byteArray = partByteArrays.get(part.getPartName().getName().substring(1));
                if (byteArray == null)
                    throw new IOException("part '" + part.getPartName() + "' not found");
                bytes = byteArray.getBytes();

            } else {

                InputStream is = sourcePartStore.loadPart(part.getPartName().getName().substring(1));
                bytes = IOUtils.toByteArray(is);
            }
        }

        // Add ZIP entry to output stream.
        if (part instanceof OleObjectBinaryPart) {
            // Workaround: Powerpoint 2010 (32-bit) can't play eg WMV if it is compressed!
            // (though 64-bit version is fine)

            ZipEntry ze = new ZipEntry(resolvedPartUri);
            ze.setMethod(ZipOutputStream.STORED);

            // must set size, compressed size, and crc-32
            ze.setSize(bytes.length);
            ze.setCompressedSize(bytes.length);

            CRC32 crc = new CRC32();
            crc.update(bytes);
            ze.setCrc(crc.getValue());

            zos.putNextEntry(ze);
        } else {
            zos.putNextEntry(new ZipEntry(resolvedPartUri));
        }

        zos.write(bytes);

        // Complete the entry
        zos.closeEntry();

    } catch (Exception e) {
        throw new Docx4JException("Failed to put binary part", e);
    }

    log.info("success writing part: " + resolvedPartUri);

}