Java Zip位元組陣列
Java在java.util.zip
包中提供了一個Adler-32
類來計算資料位元組的Adler-32
校驗和。需要呼叫這個類的update()
方法將位元組傳遞給它。
在同一個包中還有另一個名為CRC32
的類,它允許使用CRC32
演算法計算校驗和。以下程式碼演示如何使用Adler32
和CRC32
類來計算校驗和。
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);
}
}
上面的程式碼生成以下結果。
Adler32 and CRC32 checksums for HELLO
Adler32: 72089973
CRC32: 3242484790
可以使用java.util.zip
包中的Deflater
和Inflater
類來分別壓縮和解壓縮位元組陣列中的資料。
可以使用Deflater
類中的一個常數來指定壓縮級別。這些常數是BEST_COMPRESSION
,BEST_SPEED
,DEFAULT_COMPRESSION
和 NO_COMPRESSION
。
最快速度意味著較低的壓縮比,最好的壓縮意味著較慢的壓縮速度。
Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION);
預設情況下,使用Deflater
物件的壓縮資料將採用ZLIB
格式。要以GZIP
或PKZIP
格式壓縮資料,請通過在建構函式中使用布林標誌為true
來指定。
// Uses the best speed compression and GZIP format
Deflater compressor = new Deflater(Deflater.BEST_SPEED, true);
以下程式碼顯示如何使用Deflater
和Inflater
類壓縮和解壓縮位元組陣列。
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();
}
}
執行上面的程式碼,得到以下結果 -
Uncompressed data length: 12
Compressed data length: 20
Decompressed data length: 12