java 代码
- import java.io.*;
- import java.util.*;
- import org.apache.tools.zip.*;
- /**
- * added by czl
- * 2007-06-05
- * 压缩文件,支持中文路径及中文文件名
- * compressFileName(指定压缩后的文件及路径):path+"/tempfile/test.zip"
- * args(所要压缩的文件):String args[]={path+"/tempfile/download_123.txt",path+"/tempfile/download_456.txt",path+"/tempfile/download_789.txt"}
- */
- public static boolean createZip(String compressFileName,String args[]){
- boolean flag = false;
- try{
- byte b[] = new byte[512];
- ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(compressFileName));
- for(int i = 0; i < args.length; i++){
- InputStream in = new FileInputStream(args[i]);
- File file=new File(args[i]);
- String filename = file.getName();//取得文件名
- // ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/')); //压缩后带路径
- ZipEntry e = new ZipEntry(filename); //压缩后不带路径
- zout.putNextEntry(e);
- int len=0;
- while((len=in.read(b)) != -1){
- zout.write(b,0,len);
- }
- zout.closeEntry();
- }
- zout.close();
- flag = true;
- }catch(Exception e){
- e.printStackTrace();
- }
- return flag;
- }
- /**
- * added by czl
- * 2007-06-05
- * 解zip压缩文件,支持中文路径及中文文件名
- * @param zipFile
- * @param outFilePath
- * @param mode
- */
- public static boolean unZip(String zipFile,String outFilePath,int mode){
- boolean flag = false;
- try{
- File file = new File(zipFile);
- String fileName = file.getName();
- if(mode == 1)
- {
- outFilePath += File.separator; //文件当前路径下
- }else{
- outFilePath += File.separator+fileName.substring(0,fileName.length()-4)+File.separator;
- }
- File tmpFileDir = new File(outFilePath);
- tmpFileDir.mkdirs();
- ZipFile zf = new ZipFile(zipFile);
- FileOutputStream fos;
- byte[] buf = new byte[1024];
- for(Enumeration em = zf.getEntries(); em.hasMoreElements();){
- ZipEntry ze = (ZipEntry) em.nextElement();
- if(ze.isDirectory())
- {
- continue;
- }
- DataInputStream dis = new DataInputStream(zf.getInputStream(ze) );
- String currentFileName = ze.getName();
- int dex = currentFileName.lastIndexOf('/');
- String currentoutFilePath = outFilePath;
- if(dex > 0)
- {
- currentoutFilePath += currentFileName.substring(0,dex)+File.separator;
- File currentFileDir = new File(currentoutFilePath);
- currentFileDir.mkdirs();
- }
- fos = new FileOutputStream(outFilePath + ze.getName ( ));
- int readLen = 0;
- while((readLen = dis.read(buf,0,1024)) > 0 )
- {
- fos.write(buf , 0 ,readLen);
- }
- dis.close();
- fos.close();
- }
- flag = true;
- }catch(Exception e){
- e.printStackTrace();
- }
- return flag;
- }