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: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());/* w w w. j  av a2s  .  c  o m*/
        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
 *///w  w  w  . j a v  a  2s  . c  o  m
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:name.npetrovski.jphar.DataEntry.java

/**
 * Create entry from file//from w  w  w  .  j a va  2 s.c o 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.game.cs.core.condenser.steam.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object/*  ww  w .  j  av  a 2 s.c o  m*/
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    packetData = new String(packetData).substring(4).getBytes();

    return SteamPacketFactory.getPacketFromData(packetData);
}

From source file:com.github.koraktor.steamcondenser.servers.packets.SteamPacketFactory.java

/**
 * Reassembles the data of a split and/or compressed packet into a single
 * packet object/*from w  w w . ja  va 2 s  . c o m*/
 *
 * @param splitPackets An array of packet data
 * @param isCompressed whether the data of this packet is compressed
 * @param uncompressedSize The size of the decompressed packet data
 * @param packetChecksum The CRC32 checksum of the decompressed
 *        packet data
 * @throws SteamCondenserException if decompressing the packet data fails
 * @throws PacketFormatException if the calculated CRC32 checksum does not
 *         match the expected value
 * @return SteamPacket The reassembled packet
 * @see SteamPacketFactory#getPacketFromData
 */
public static SteamPacket reassemblePacket(ArrayList<byte[]> splitPackets, boolean isCompressed,
        int uncompressedSize, int packetChecksum) throws SteamCondenserException {
    byte[] packetData, tmpData;
    packetData = new byte[0];

    for (byte[] splitPacket : splitPackets) {
        tmpData = packetData;
        packetData = new byte[tmpData.length + splitPacket.length];
        System.arraycopy(tmpData, 0, packetData, 0, tmpData.length);
        System.arraycopy(splitPacket, 0, packetData, tmpData.length, splitPacket.length);
    }

    if (isCompressed) {
        try {
            ByteArrayInputStream stream = new ByteArrayInputStream(packetData);
            stream.read();
            stream.read();
            BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(stream);
            byte[] uncompressedPacketData = new byte[uncompressedSize];
            bzip2.read(uncompressedPacketData, 0, uncompressedSize);

            CRC32 crc32 = new CRC32();
            crc32.update(uncompressedPacketData);
            int crc32checksum = (int) crc32.getValue();

            if (crc32checksum != packetChecksum) {
                throw new PacketFormatException("CRC32 checksum mismatch of uncompressed packet data.");
            }
            packetData = uncompressedPacketData;
        } catch (IOException e) {
            throw new SteamCondenserException(e.getMessage(), e);
        }
    }

    tmpData = packetData;
    packetData = new byte[tmpData.length - 4];
    System.arraycopy(tmpData, 4, packetData, 0, tmpData.length - 4);

    return SteamPacketFactory.getPacketFromData(packetData);
}

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

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

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

public static long getCrc(byte[] data) {
    CRC32 crc = new CRC32();
    crc.update(data);/*from  w w  w.  j a  v  a 2 s. co  m*/
    return crc.getValue();
}

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);/*from   ww  w.  ja  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:PNGDecoder.java

protected static boolean verifyCRC(byte[] typeBytes, byte[] data, long crc) {
    CRC32 crc32 = new CRC32();
    crc32.update(typeBytes);/* w w  w.jav a  2  s  .c om*/
    crc32.update(data);
    long calculated = crc32.getValue();
    return (calculated == crc);
}

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

/**
 *
 * @param f//from  w w  w  .  java  2  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();

}