📄 ioutil.java
字号:
package cn.dxm.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
/**
* I/O操作工具类
* @author hdf
*/
public class IOUtil {
/**
* 对字节流进行压缩
* @param input:要压缩的字节流
* @param level:级别 9~1
* @return
*/
public static byte[] compress(byte[] input, int level) {
// Create the compressor with highest level of compression
Deflater compressor = new Deflater();
compressor.setLevel(level);
// Give the compressor the data to compress
compressor.setInput(input);
compressor.finish();
// Create an expandable byte array to hold the compressed data.
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
// Compress the data
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.getStackTrace();
}
// Get the compressed data
return bos.toByteArray();
}
/**
* 解压缩字节流
* @param compressedData
* @return
*/
public static byte[] decompress (byte[] compressedData) {
// Create the decompressor and give it the data to compress
Inflater decompressor = new Inflater();
decompressor.setInput(compressedData);
// Create an expandable byte array to hold the decompressed data
ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
// Decompress the data
byte[] buf = new byte[1024];
while (!decompressor.finished()) {
try {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
} catch (DataFormatException e) {
e.getStackTrace();
}
}
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.getStackTrace();
}
// Get the decompressed data
return bos.toByteArray();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -