package jp.biziq.sxf.zip;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.codec.binary.Base64;
public class ZipCompress {
public static void main(String[] args) {
// File file = new File("d:\\search_user_max_create_zip.xml");
// File file = new File("d:\\abc\\search_user_max.xml");
File file = new File("d:\\abc\\offer_max.xml");
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
StringBuffer sb = new StringBuffer("");
String str = null;
while((str = bfr.readLine()) != null) {
sb.append(str);
sb.append("\r\n");
}
bfr.close();
fr.close();
// System.out.println(sb.toString());
long startTime = System.currentTimeMillis();
String result = ZipCompress.compress(sb.toString());
System.out.println("压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
String deCompress = ZipCompress.decompress(result);
System.out.println("解压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
// OutputStream os = new FileOutputStream("d:\\compress_users_string.xml");
// os.write(result.getBytes(), 0, result.getBytes().length);
// System.out.println(deCompress);
FileWriter fw = new FileWriter("d:\\compress_users_string_fw.xml");
fw.write(result);
// os.close();
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*String str = "奥术法术的方式地方史蒂夫23423sss222xxx || 奥术法术的方式地方史蒂夫23423sss222xxx";
String compress = ZipCompress.compress(str);
System.out.println(compress);
System.out.println(ZipCompress.decompress(compress));*/
/*String str1 = "是的防守打法xxxs234234";
String str2 = "test蛤蟆??";
byte[] compress1 = ZipCompress.compress(str1);
byte[] compress2 = ZipCompress.compress(str2);
// for(byte b : compress1) {
// System.out.print((char)b);
// }
// System.out.println();
// for(byte b : compress2) {
// System.out.print((char)b);
// }
System.out.println("compress1: " + compress1);
System.out.println("解压compress1:" + ZipCompress.decompress(compress1));
System.out.println("解压compress2:" + ZipCompress.decompress(compress2));
byte[] sum = new byte[compress1.length + compress2.length];
System.arraycopy(compress1, 0, sum, 0, compress1.length);
System.arraycopy(compress2, 0, sum, compress1.length, compress2.length);
for(byte b : sum) {
System.out.print((char)b);
}
System.out.println();
System.out.println("解压:" + ZipCompress.decompress(sum));*/
/*File file = new File("d:\\search_user_max_create_zip.xml");
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
StringBuffer sb = new StringBuffer("");
String str = null;
while((str = bfr.readLine()) != null) {
sb.append(str);
sb.append("\r\n");
}
bfr.close();
fr.close();
// System.out.println(sb.toString());
long startTime = System.currentTimeMillis();
byte[] result = ZipCompress.compress(sb.toString());
System.out.println("压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
ZipCompress.decompress(result);
System.out.println("解压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
OutputStream os = new FileOutputStream("d:\\compress_users.xml");
os.write(result, 0, result.length);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
/**
* 压缩字符串为 byte[]
* 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法
* 保存为字符串
*
* @param str 压缩前的文本
* @return
*/
public static final String compress(String str) {
if(str == null) {
return null;
}
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch(IOException e) {
e.printStackTrace();
compressed = null;
} finally {
if(zout != null) {
try{zout.close();} catch(IOException e){}
}
if(out != null) {
try{out.close();} catch(IOException e){}
}
}
if(compressed != null) {
return Base64.encodeBase64String(compressed);
}
return null;
}
/**
* 将压缩后的 byte[] 数据解压缩
*
* @param compressed 压缩后的 byte[] 数据
* @return 解压后的字符串
*/
public static final String decompress(String str) {
byte[] compressed = Base64.decodeBase64(str);
if(compressed == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
e.printStackTrace();
decompressed = null;
} finally {
if(zin != null) {
try {zin.close();} catch(IOException e) {}
}
if(in != null) {
try {in.close();} catch(IOException e) {}
}
if(out != null) {
try {out.close();} catch(IOException e) {}
}
}
return decompressed;
}
}
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.apache.commons.codec.binary.Base64;
public class ZipCompress {
public static void main(String[] args) {
// File file = new File("d:\\search_user_max_create_zip.xml");
// File file = new File("d:\\abc\\search_user_max.xml");
File file = new File("d:\\abc\\offer_max.xml");
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
StringBuffer sb = new StringBuffer("");
String str = null;
while((str = bfr.readLine()) != null) {
sb.append(str);
sb.append("\r\n");
}
bfr.close();
fr.close();
// System.out.println(sb.toString());
long startTime = System.currentTimeMillis();
String result = ZipCompress.compress(sb.toString());
System.out.println("压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
String deCompress = ZipCompress.decompress(result);
System.out.println("解压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
// OutputStream os = new FileOutputStream("d:\\compress_users_string.xml");
// os.write(result.getBytes(), 0, result.getBytes().length);
// System.out.println(deCompress);
FileWriter fw = new FileWriter("d:\\compress_users_string_fw.xml");
fw.write(result);
// os.close();
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*String str = "奥术法术的方式地方史蒂夫23423sss222xxx || 奥术法术的方式地方史蒂夫23423sss222xxx";
String compress = ZipCompress.compress(str);
System.out.println(compress);
System.out.println(ZipCompress.decompress(compress));*/
/*String str1 = "是的防守打法xxxs234234";
String str2 = "test蛤蟆??";
byte[] compress1 = ZipCompress.compress(str1);
byte[] compress2 = ZipCompress.compress(str2);
// for(byte b : compress1) {
// System.out.print((char)b);
// }
// System.out.println();
// for(byte b : compress2) {
// System.out.print((char)b);
// }
System.out.println("compress1: " + compress1);
System.out.println("解压compress1:" + ZipCompress.decompress(compress1));
System.out.println("解压compress2:" + ZipCompress.decompress(compress2));
byte[] sum = new byte[compress1.length + compress2.length];
System.arraycopy(compress1, 0, sum, 0, compress1.length);
System.arraycopy(compress2, 0, sum, compress1.length, compress2.length);
for(byte b : sum) {
System.out.print((char)b);
}
System.out.println();
System.out.println("解压:" + ZipCompress.decompress(sum));*/
/*File file = new File("d:\\search_user_max_create_zip.xml");
try {
FileReader fr = new FileReader(file);
BufferedReader bfr = new BufferedReader(fr);
StringBuffer sb = new StringBuffer("");
String str = null;
while((str = bfr.readLine()) != null) {
sb.append(str);
sb.append("\r\n");
}
bfr.close();
fr.close();
// System.out.println(sb.toString());
long startTime = System.currentTimeMillis();
byte[] result = ZipCompress.compress(sb.toString());
System.out.println("压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
startTime = System.currentTimeMillis();
ZipCompress.decompress(result);
System.out.println("解压缩耗时:" + (System.currentTimeMillis() - startTime) + "ms");
OutputStream os = new FileOutputStream("d:\\compress_users.xml");
os.write(result, 0, result.length);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
}
/**
* 压缩字符串为 byte[]
* 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法
* 保存为字符串
*
* @param str 压缩前的文本
* @return
*/
public static final String compress(String str) {
if(str == null) {
return null;
}
byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;
try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch(IOException e) {
e.printStackTrace();
compressed = null;
} finally {
if(zout != null) {
try{zout.close();} catch(IOException e){}
}
if(out != null) {
try{out.close();} catch(IOException e){}
}
}
if(compressed != null) {
return Base64.encodeBase64String(compressed);
}
return null;
}
/**
* 将压缩后的 byte[] 数据解压缩
*
* @param compressed 压缩后的 byte[] 数据
* @return 解压后的字符串
*/
public static final String decompress(String str) {
byte[] compressed = Base64.decodeBase64(str);
if(compressed == null) {
return null;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
e.printStackTrace();
decompressed = null;
} finally {
if(zin != null) {
try {zin.close();} catch(IOException e) {}
}
if(in != null) {
try {in.close();} catch(IOException e) {}
}
if(out != null) {
try {out.close();} catch(IOException e) {}
}
}
return decompressed;
}
}