package test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterOutputStream;
import org.xerial.snappy.Snappy;
import net.jpountz.lz4.LZ4BlockOutputStream;
import net.jpountz.lz4.LZ4Compressor;
import net.jpountz.lz4.LZ4Factory;
public class TestZip {
static ByteBuffer buffer=ByteBuffer.allocate(20);
public static long bytesToLong(byte[] bytes) {
buffer.put(bytes, 0, bytes.length);
buffer.flip();//need flip
return buffer.getLong();
}
public static byte[] longToBytes(long x) {
buffer.putLong(0, x);
return buffer.array();
}
public static void main(String[] args) throws UnsupportedEncodingException {
String str="snowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zl";
String str1="adsfsaasdfsnowolf@zlex.org;dongliang@zlex.org;zlex.dongliang@zl";
System.out.println(str);
byte [] aa=str.getBytes("UTF-8");
byte [] aa1=str1.getBytes("UTF-8");
System.out.println(aa.length);
byte [] bb=lengthCp(aa);
byte [] bb1=lengthCp(aa1);
byte[] data3 = new byte[bb.length + bb1.length];
//System.arraycopy(aa,0,data3,0,aa.length);
// System.arraycopy(aa1,0,data3,aa.length,aa1.length);
// System.arraycopy(bb,0,data3,0,bb.length);
System.arraycopy(bb1,0,data3,0,bb1.length);
System.out.println(bb.length);
System.out.println(data3.length);
lengthDp(data3);
}
public static byte[] lengthCp(byte[] datas){
try {
ByteBuffer buffer = ByteBuffer.allocate(datas.length);
buffer.put(datas);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DeflaterOutputStream dos = new DeflaterOutputStream(baos);
dos.write(buffer.array());
dos.finish();
int len = buffer.array().length;
ByteBuffer temp = ByteBuffer
.allocate(baos.toByteArray().length);// 压缩前的数据长度
//temp.put(intToByteArray(len));//长度校验
temp.put(baos.toByteArray());
return temp.array();
} catch (Exception e) {
e.printStackTrace();
}
return new byte[0];
}
public static void lengthDp(byte[] datas ){
/* byte[] datas =Arrays.copyOfRange(data, 4, data.length);
byte[] dat = Arrays.copyOfRange(data, 0, 4);*/
try {
/* int a=byteArrayToInt(dat);
System.out.println(a); */
ByteBuffer buffer = ByteBuffer.allocate(datas.length);
buffer.put(datas);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InflaterOutputStream dos = new InflaterOutputStream(baos);
dos.write(buffer.array());
dos.finish();
ByteBuffer temp = ByteBuffer
.allocate(baos.toByteArray().length);// 解压数据长度
temp.put(baos.toByteArray());
System.out.println(new String(temp.array()));
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] lz4Compress(byte[] data) throws IOException {
LZ4Factory factory = LZ4Factory.fastestInstance();
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
LZ4Compressor compressor = factory.fastCompressor();
LZ4BlockOutputStream compressedOutput = new LZ4BlockOutputStream(byteOutput, 8192, compressor);
compressedOutput.write(data);
compressedOutput.close();
return byteOutput.toByteArray();
}
//snappy 压缩
public static byte[] snappyCompress(byte[] data) throws IOException {
return Snappy.compress(data);
}
public static byte[] snappyDecompress(byte[] data) throws IOException {
return Snappy.uncompress(data);
}
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static byte[] intToByteArray(int a) {
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
private void compressBillNo(byte[] buf, String s) {
int b1, b2;
for (int i = 0; i < 6; i++) {
b1 = s.charAt(i * 2) - 48;//48即为'0'
b2 = s.charAt(i * 2 + 1) - 48;
buf[i] = (byte) ((b1 << 4) | b2);
}
}
}