JAVA中实现数据压缩所有方式(适用于通信时数据压缩)
2021-06-21 本文已影响0人
小胖学编程
在网络传输或者数据缓存时,可以对数据进行压缩。本文将实现JAVA中常用的7种压缩和解压缩的方式。
- BZip方式
1.1 引入依赖
1.2 BZip工具类代码
1.3 BZip2工具类代码 - Deflater方式
- Gzip方式
- Lz4方式
4.1 引入依赖
4.2 工具类代码 - SevenZ方式
5.1 引入依赖
5.2 工具类代码 - Zip方式
1. BZip方式
1.1 引入依赖
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.6</version>
</dependency>
1.2 BZip工具类代码
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.bzip2.CBZip2OutputStream;
public class BZip2Util {
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos)) {
bzip2.write(bytes);
bzip2.finish();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("BZip2 compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
try (CBZip2InputStream bzip2 = new CBZip2InputStream(bis)) {
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = bzip2.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("BZip2 decompress error", e);
}
}
}
1.3 BZip2工具类代码
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.tools.bzip2.CBZip2InputStream;
import org.apache.tools.bzip2.CBZip2OutputStream;
public class BZip2Util {
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos)) {
bzip2.write(bytes);
bzip2.finish();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("BZip2 compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
try (CBZip2InputStream bzip2 = new CBZip2InputStream(bis)) {
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = bzip2.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("BZip2 decompress error", e);
}
}
}
2. Deflater方式
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class DeflaterUtil {
private DeflaterUtil() {
}
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
int lenght = 0;
Deflater deflater = new Deflater();
deflater.setInput(bytes);
deflater.finish();
byte[] outputBytes = new byte[BUFFER_SIZE];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
while (!deflater.finished()) {
lenght = deflater.deflate(outputBytes);
bos.write(outputBytes, 0, lenght);
}
deflater.end();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Deflater compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
int length = 0;
Inflater inflater = new Inflater();
inflater.setInput(bytes);
byte[] outputBytes = new byte[BUFFER_SIZE];
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
while (!inflater.finished()) {
length = inflater.inflate(outputBytes);
if (length == 0) {
break;
}
bos.write(outputBytes, 0, length);
}
inflater.end();
return bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("Deflater decompress error", e);
}
}
}
3. Gzip方式
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GzipUtil {
private GzipUtil() {
}
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
gzip.write(bytes);
gzip.flush();
gzip.finish();
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("gzip compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (GZIPInputStream gunzip = new GZIPInputStream(new ByteArrayInputStream(bytes))) {
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = gunzip.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("gzip decompress error", e);
}
}
}
4. Lz4方式
4.1 引入依赖
<dependency>
<groupId>org.lz4</groupId>
<artifactId>lz4-java</artifactId>
<version>1.7.1</version>
</dependency>
4.2 工具类代码
public class Lz4Util {
private static final Logger LOGGER = LoggerFactory.getLogger(Lz4Util.class);
private static final int ARRAY_SIZE = 1024;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
LZ4Compressor compressor = LZ4Factory.fastestInstance().fastCompressor();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (LZ4BlockOutputStream lz4BlockOutputStream
= new LZ4BlockOutputStream(outputStream, ARRAY_SIZE, compressor)) {
lz4BlockOutputStream.write(bytes);
} catch (IOException e) {
LOGGER.error("compress bytes error", e);
}
return outputStream.toByteArray();
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(ARRAY_SIZE);
LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
try (LZ4BlockInputStream decompressedInputStream
= new LZ4BlockInputStream(inputStream, decompressor)) {
int count;
byte[] buffer = new byte[ARRAY_SIZE];
while ((count = decompressedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, count);
}
} catch (IOException e) {
LOGGER.error("decompress bytes error", e);
}
return outputStream.toByteArray();
}
}
5. SevenZ方式
5.1 引入依赖
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.19</version>
</dependency>
5.2 工具类代码
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class SevenZUtil {
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel();
try (SevenZOutputFile z7z = new SevenZOutputFile(channel)) {
SevenZArchiveEntry entry = new SevenZArchiveEntry();
entry.setName("sevenZip");
entry.setSize(bytes.length);
z7z.putArchiveEntry(entry);
z7z.write(bytes);
z7z.closeArchiveEntry();
z7z.finish();
return channel.array();
} catch (IOException e) {
throw new RuntimeException("SevenZ compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(bytes);
try (SevenZFile sevenZFile = new SevenZFile(channel)) {
byte[] buffer = new byte[BUFFER_SIZE];
while (sevenZFile.getNextEntry() != null) {
int n;
while ((n = sevenZFile.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("SevenZ decompress error", e);
}
}
}
6. Zip方式
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtil {
private static final int BUFFER_SIZE = 8192;
public static byte[] compress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ZipOutputStream zip = new ZipOutputStream(out)) {
ZipEntry entry = new ZipEntry("zip");
entry.setSize(bytes.length);
zip.putNextEntry(entry);
zip.write(bytes);
zip.closeEntry();
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Zip compress error", e);
}
}
public static byte[] decompress(byte[] bytes) {
if (bytes == null) {
throw new NullPointerException("bytes is null");
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(bytes))) {
byte[] buffer = new byte[BUFFER_SIZE];
while (zip.getNextEntry() != null) {
int n;
while ((n = zip.read(buffer)) > -1) {
out.write(buffer, 0, n);
}
}
return out.toByteArray();
} catch (IOException e) {
throw new RuntimeException("Zip decompress error", e);
}
}
}