java的zip压缩(转自Jeet)
public static String compress(String s) throws IOException{
ByteArrayInputStream input = new ByteArrayInputStream(s.getBytes("UTF-8"));
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
GZIPOutputStream gzout = new GZIPOutputStream(output);
byte[] buf=new byte[1024];
int number;
while ((number = input.read(buf)) != -1){
gzout.write(buf,0,number);
}
gzout.close();
input.close();
String result =new BASE64Encoder().encode(output.toByteArray());
output.close();
return result;
}
public static String decompress(String data) throws IOException{
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
ByteArrayInputStream input = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(data));
GZIPInputStream gzinpt = new GZIPInputStream(input);
byte[] buf = new byte[1024];
int number = 0;
while((number = gzinpt.read(buf)) != -1){
output.write(buf,0,number);
}
gzinpt.close();
input.close();
String result = new String(output.toString("UTF-8"));
output.close();
return result;
}
ByteArrayInputStream input = new ByteArrayInputStream(s.getBytes("UTF-8"));
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
GZIPOutputStream gzout = new GZIPOutputStream(output);
byte[] buf=new byte[1024];
int number;
while ((number = input.read(buf)) != -1){
gzout.write(buf,0,number);
}
gzout.close();
input.close();
String result =new BASE64Encoder().encode(output.toByteArray());
output.close();
return result;
}
public static String decompress(String data) throws IOException{
ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
ByteArrayInputStream input = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(data));
GZIPInputStream gzinpt = new GZIPInputStream(input);
byte[] buf = new byte[1024];
int number = 0;
while((number = gzinpt.read(buf)) != -1){
output.write(buf,0,number);
}
gzinpt.close();
input.close();
String result = new String(output.toString("UTF-8"));
output.close();
return result;
}