BufferedInputStream示例
BufferedOutputStream示例
BufferedReader實例
BufferedWriter示例
ByteArrayInputStream示例
Java ByteArrayOutputStream示例
Java CharArrayReader示例
Java CharArrayWriter示例
Java Console類示例
Java DataInputStream類型
Java DataOutputStream實例
Java Externalizable實例
Java File實例
FileDescriptor示例
Java FileInputStream示例
FileOutputStream示例
Java FilePermission示例
Java FileReader示例
Java FileWriter示例
Java FilterInputStream示例
Java FilterOutputStream示例
Java FilterReader示例
Java FilterWriter示例
Java Zip字節數組
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