Example usage for java.util.zip CRC32 update

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

Introduction

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

Prototype

@Override
public void update(ByteBuffer buffer) 

Source Link

Document

Updates the CRC-32 checksum with the bytes from the specified buffer.

Usage

From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java

private static void storeOpenDocumentSpreadsheetFile(File file, InputStream source) throws Exception {

    try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {

        //addResourceFile("mimetype", "/resource/ods/mimetype", out);
        ZipEntry ze = new ZipEntry("mimetype");
        String mime = "application/vnd.oasis.opendocument.spreadsheet";
        ze.setMethod(ZipEntry.STORED);
        ze.setSize(mime.length());//from  w  w w.  j  ava 2  s  . c  om
        CRC32 crc = new CRC32();
        crc.update(mime.getBytes());
        ze.setCrc(crc.getValue());
        out.putNextEntry(ze);
        for (int i = 0; i < mime.length(); i++) {
            out.write(mime.charAt(i));
        }
        out.closeEntry();

        ZipEntry zipEntry = new ZipEntry("content.xml");
        //zipEntry.setMethod(ZipEntry.DEFLATED);
        out.putNextEntry(zipEntry);
        int c;
        while ((c = source.read()) >= 0) {
            out.write(c);
        }
        out.closeEntry();

        // Add manifest (required for OOo 2.0) and "meta.xml": These are in the
        // resource/ods directory, and are copied verbatim into the zip file.
        OpenDocumentSpreadsheetCreator.addResourceFile("meta.xml", "/resource/ods/meta.xml", out);

        OpenDocumentSpreadsheetCreator.addResourceFile("META-INF/manifest.xml", "/resource/ods/manifest.xml",
                out);
    }
}

From source file:com.zimbra.cs.util.ZipUtil.java

/**
 * Use InfoZIP Unicode Extra Fields (if present) to set the filename
 *//*ww  w. java  2  s. c om*/
private static String getNameFromUnicodeExtraPathIfPresent(ZipArchiveEntry zae) {
    UnicodePathExtraField unicodePathExtraField = (UnicodePathExtraField) zae
            .getExtraField(UnicodePathExtraField.UPATH_ID);
    if (null == unicodePathExtraField) {
        return null;
    }
    CRC32 crc32 = new CRC32();
    crc32.update(zae.getRawName());
    long origCRC32 = crc32.getValue();

    if (origCRC32 == unicodePathExtraField.getNameCRC32()) {
        String val = convertBytesIfPossible(unicodePathExtraField.getUnicodeName(), StandardCharsets.UTF_8);
        if (null != val) {
            ZimbraLog.misc.debug("ZipUtil name '%s' from unicodeExtraPath", val);
        }
        return val;
    }
    return null;
}

From source file:PNGDecoder.java

protected static boolean verifyCRC(byte[] typeBytes, byte[] data, long crc) {
    CRC32 crc32 = new CRC32();
    crc32.update(typeBytes);
    crc32.update(data);//w  w w  .j a  va 2 s.  co m
    long calculated = crc32.getValue();
    return (calculated == crc);
}

From source file:name.npetrovski.jphar.DataEntry.java

/**
 * Create entry from file//w w  w.  j a va2 s . co m
 *
 */
public static DataEntry createFromFile(File file, Compression.Type compression) throws IOException {

    EntryManifest em = new EntryManifest();
    em.setCompression(new Compression(compression));
    em.setTimestamp((int) file.lastModified() / 1000);
    em.getPath().setName(file.toPath().toString().replace("\\", "/"));

    DataEntry entry = new DataEntry(em);
    entry.setSource(file.getCanonicalFile());

    if (file.isDirectory()) {
        em.getCompression().setType(Compression.Type.NONE);
    } else {
        byte[] data = Files.readAllBytes(file.toPath());
        CRC32 crc = new CRC32();
        crc.update(data);

        em.setCRC32((int) crc.getValue());
        em.setUncompressedSize(data.length);
    }

    return entry;

}

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

public static long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);// ww w  . j a  v a 2 s . c  o m
    int b;
    while ((b = stm.read()) >= 0) {
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:me.j360.dubbo.modules.util.text.HashUtil.java

/**
 * crc32php64bitlong//w ww.j  a  v  a  2 s . c o m
 * 
 * Guavacrc32, longJDK
 */
public static long crc32AsLong(@NotNull byte[] input) {
    CRC32 crc32 = new CRC32();
    crc32.update(input);
    return crc32.getValue();
}

From source file:me.j360.dubbo.modules.util.text.HashUtil.java

/**
 * crc32int, ?./*from  w w w.  ja  v  a 2 s. c o m*/
 * 
 * Guavacrc32, longJDK
 */
public static int crc32AsInt(@NotNull byte[] input) {
    CRC32 crc32 = new CRC32();
    crc32.update(input);
    // CRC32 ? 32bit intCheckSum??long??
    return (int) crc32.getValue();
}

From source file:io.blobkeeper.file.util.FileUtils.java

public static long getCrc(byte[] data) {
    CRC32 crc = new CRC32();
    crc.update(data);
    return crc.getValue();
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get a directory for a given resource identifier.
 * @param baseDirectory the base directory
 * @param identifier a resource identifier
 * @return a directory//w ww.j a  va  2 s.  c om
 */
public static File getResourceDirectory(final File baseDirectory, final IRI identifier) {
    requireNonNull(baseDirectory, "The baseDirectory may not be null!");
    requireNonNull(identifier, "The identifier may not be null!");
    final String id = identifier.getIRIString();
    final StringJoiner joiner = new StringJoiner(separator);
    final CRC32 hasher = new CRC32();
    hasher.update(id.getBytes(UTF_8));
    final String intermediate = Long.toHexString(hasher.getValue());

    range(0, intermediate.length() / LENGTH).limit(MAX)
            .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH)));

    joiner.add(md5Hex(id));
    return new File(baseDirectory, joiner.toString());
}

From source file:eionet.gdem.utils.ZipUtil.java

/**
 *
 * @param f/*from ww  w .  j a  va2 s  .c o m*/
 *            - File that will be zipped
 * @param outZip
 *            - ZipOutputStream represents the zip file, where the files will be placed
 * @param sourceDir
 *            - root directory, where the zipping started
 * @param doCompress
 *            - don't comress the file, if doCompress=true
 * @throws IOException If an error occurs.
 */
public static void zipFile(File f, ZipOutputStream outZip, String sourceDir, boolean doCompress)
        throws IOException {

    // Read the source file into byte array
    byte[] fileBytes = Utils.getBytesFromFile(f);

    // create a new zip entry
    String strAbsPath = f.getPath();
    String strZipEntryName = strAbsPath.substring(sourceDir.length() + 1, strAbsPath.length());
    strZipEntryName = Utils.Replace(strZipEntryName, File.separator, "/");
    ZipEntry anEntry = new ZipEntry(strZipEntryName);

    // Don't compress the file, if not needed
    if (!doCompress) {
        // ZipEntry can't calculate crc size automatically, if we use STORED method.
        anEntry.setMethod(ZipEntry.STORED);
        anEntry.setSize(fileBytes.length);
        CRC32 crc321 = new CRC32();
        crc321.update(fileBytes);
        anEntry.setCrc(crc321.getValue());
    }
    // place the zip entry in the ZipOutputStream object
    outZip.putNextEntry(anEntry);

    // now write the content of the file to the ZipOutputStream
    outZip.write(fileBytes);

    outZip.flush();
    // Close the current entry
    outZip.closeEntry();

}