W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
Java在java.util.zip包中提供了一個(gè)Adler32類來計(jì)算數(shù)據(jù)字節(jié)的Adler-32校驗(yàn)和。
我們需要調(diào)用這個(gè)類的update()方法將字節(jié)傳遞給它。
在同一個(gè)包中還有另一個(gè)名為CRC32的類,它允許您使用CRC32算法計(jì)算校驗(yàn)和。
以下代碼說明如何使用Adler32和CRC32類來計(jì)算校驗(yàn)和。
import java.util.zip.Adler32; import java.util.zip.CRC32; public class Main { public static void main(String[] args) throws Exception { String str = "HELLO"; byte[] data = str.getBytes("UTF-8"); System.out.println("Adler32 and CRC32 checksums for " + str); // Compute Adler32 checksum Adler32 ad = new Adler32(); ad.update(data); long adler32Checksum = ad.getValue(); System.out.println("Adler32: " + adler32Checksum); // Compute CRC32 checksum CRC32 crc = new CRC32(); crc.update(data); long crc32Checksum = crc.getValue(); System.out.println("CRC32: " + crc32Checksum); } }
上面的代碼生成以下結(jié)果。
我們可以使用java.util.zip包中的Deflater和Inflater類來分別壓縮和解壓縮字節(jié)數(shù)組中的數(shù)據(jù)。
我們可以使用Deflater類中的一個(gè)常量來指定壓縮級(jí)別。
這些常數(shù)是BEST_COMPRESSION,BEST_ SPEED,DEFAULT_COMPRESSION和NO_COMPRESSION。
最佳速度意味著較低的壓縮比,最好的壓縮意味著較慢的壓縮速度。
Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
默認(rèn)情況下,壓縮數(shù)據(jù)使用Deflater對(duì)象將以ZLIB格式。
要以GZIP或PKZIP格式壓縮數(shù)據(jù),請(qǐng)通過在構(gòu)造函數(shù)中使用布爾標(biāo)志為true來指定。
// Uses the best speed compression and GZIP format Deflater compressor = new Deflater(Deflater.BEST_SPEED, true);
以下代碼顯示如何使用Deflater和Inflater類壓縮和解壓縮字節(jié)數(shù)組
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; public class Main { public static void main(String[] args) throws Exception { String input = "Hello world!"; byte[] uncompressedData = input.getBytes("UTF-8"); byte[] compressedData = compress(uncompressedData, Deflater.BEST_COMPRESSION, false); byte[] decompressedData = decompress(compressedData, false); String output = new String(decompressedData, "UTF-8"); System.out.println("Uncompressed data length: " + uncompressedData.length); System.out.println("Compressed data length: " + compressedData.length); System.out.println("Decompressed data length: " + decompressedData.length); } public static byte[] compress(byte[] input, int compressionLevel, boolean GZIPFormat) throws IOException { Deflater compressor = new Deflater(compressionLevel, GZIPFormat); compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); byte[] readBuffer = new byte[1024]; int readCount = 0; while (!compressor.finished()) { readCount = compressor.deflate(readBuffer); if (readCount > 0) { bao.write(readBuffer, 0, readCount); } } compressor.end(); return bao.toByteArray(); } public static byte[] decompress(byte[] input, boolean GZIPFormat) throws IOException, DataFormatException { Inflater decompressor = new Inflater(GZIPFormat); decompressor.setInput(input); ByteArrayOutputStream bao = new ByteArrayOutputStream(); byte[] readBuffer = new byte[1024]; int readCount = 0; while (!decompressor.finished()) { readCount = decompressor.inflate(readBuffer); if (readCount > 0) { bao.write(readBuffer, 0, readCount); } } decompressor.end(); return bao.toByteArray(); } }
上面的代碼生成以下結(jié)果。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: