Java Zip字节数组

Java IO教程 - Java Zip字节数组

校验和

Java在java.util.zip包中提供了一个Adler32类来计算数据字节的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);
  }
}

上面的代码生成以下结果。


压缩字节数组

我们可以使用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();
  }
}

上面的代码生成以下结果。