📄 ziputil.java
字号:
/**
* by jansonhorn 2007.12.28
*/
import java.util.zip.*;
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ZipUtil{
public static void zip(String zipFileName,String inputFile)throws Exception{
zip(zipFileName,new File(inputFile));
}
public static void zip(String zipFileName,File inputFile)throws Exception{
ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipFileName));
zip(out,inputFile,"");
out.close();
}
public static void unzip(String zipFileName,String outputDirectory)throws Exception{
ZipInputStream in=new ZipInputStream(new FileInputStream(zipFileName));
File d = new File(outputDirectory);
if(!d.exists())d.mkdir();
ZipEntry z;
while ((z=in.getNextEntry() )!= null){
System.out.println("unziping "+z.getName());
if (z.isDirectory()){
String name=z.getName();
name=name.substring(0,name.length()-1);
File f=new File(outputDirectory+File.separator+name);
f.mkdir();
}else{
File f=new File(outputDirectory+File.separator+z.getName());
//System.out.println("create new file: " + outputDirectory+File.separator+z.getName());
f.createNewFile();
FileOutputStream out=new FileOutputStream(f);
int b;
while ((b=in.read()) != -1)
out.write(b);
out.close();
}
}
in.close();
}
public static void zip(ZipOutputStream out,File f,String base)throws Exception{
if (f.isDirectory()){
File[] fl=f.listFiles();
out.putNextEntry(new ZipEntry(base+"/"));
base=base.length()==0?"":base+"/";
for (int i=0;i<fl.length ;i++ ){
zip(out,fl[i],base+fl[i].getName());
}
}else{
out.putNextEntry(new ZipEntry(base));
FileInputStream in=new FileInputStream(f);
int b;
while ((b=in.read()) != -1)
out.write(b);
in.close();
}
}
private static byte[] _unzip(byte[] zipbytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(zipbytes);
// string entryname = new string("servletservice");
ZipInputStream zis = new ZipInputStream(bais);
zis.getNextEntry();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final int bufsiz = 4096;
byte inbuf[] = new byte[bufsiz];
int n;
while ( (n = zis.read(inbuf, 0, bufsiz)) != -1) {
baos.write(inbuf, 0, n);
}
byte[] data = baos.toByteArray();
zis.close();
return data;
}
private static byte[] _zip(byte[] data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipEntry ze = new ZipEntry("xiaoshuoss");
ZipOutputStream zos = new ZipOutputStream(baos);
zos.putNextEntry(ze);
zos.write(data, 0, data.length);
zos.close();
byte[] zipbytes = baos.toByteArray();
return zipbytes;
}
public static String zip(String s) throws IOException{
byte[] bt = s.getBytes();
byte[] bt1 = _zip(bt);
String b = new BASE64Encoder().encode(bt1);
return b;
}
public static String unzip(String s) throws IOException{
byte[] bt = new BASE64Decoder().decodeBuffer(s);
byte[] bt1 = _unzip(bt);
String c = new String(bt1);
return c;
}
public static void main(String[] args) throws IOException{
try{
unzip("c:\\temp.zip","c:\\temp");
}
catch(Exception e){e.printStackTrace(System.out);}
String s = "just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test." +
"just a test, 这是一个测试,this is a test.";
String b = zip(s);
System.out.println(b);
String c = unzip(b);
System.out.println(c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -