Java Zip字節數組

Java Zip字節數組

Java在java.util.zip包中提供了一個Adler-32類來計算數據字節的Adler-32校驗和。需要調用這個類的update()方法將字節傳遞給它。

在同一個包中還有另一個名爲CRC32的類,它允許使用CRC32算法計算校驗和。以下代碼演示如何使用Adler32CRC32類來計算校驗和。

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包中的DeflaterInflater類來分別壓縮和解壓縮字節數組中的數據。

可以使用Deflater類中的一個常量來指定壓縮級別。這些常數是BEST_COMPRESSIONBEST_SPEEDDEFAULT_COMPRESSIONNO_COMPRESSION
最快速度意味着較低的壓縮比,最好的壓縮意味着較慢的壓縮速度。

Deflater  compressor = new Deflater(Deflater.BEST_COMPRESSION);

默認情況下,使用Deflater對象的壓縮數據將採用ZLIB格式。要以GZIPPKZIP格式壓縮數據,請通過在構造函數中使用布爾標誌爲true來指定。

// Uses  the   best speed  compression and  GZIP format
Deflater  compressor = new Deflater(Deflater.BEST_SPEED, true);

以下代碼顯示如何使用DeflaterInflater類壓縮和解壓縮字節數組。

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