您的位置:首页 > 其它

zip

2016-02-29 18:59 357 查看
import java.util.*;

import java.io.*;

import org.apache.tools.zip.*;

/**

* Author:lixiaoning

* Date:2009-4-7

*/

public class Zipper

{

private static String unrarCmd = "C:\\Program Files\\WinRAR\\UnRar x ";

/**

* 压缩文件,支持中文路径及中文文件名 支持rar格式以及zip格式

* 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;

}

/**

* 解压文件,支持中文路径及中文文件名 支持rar以及zip格式

* @param zipFile

* @param outFilePath

* @param mode

*/

public static boolean unfile(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();

String unfile=zipFile.substring(zipFile.length()-3);

if(unfile=="zip"||unfile.equals("zip")){

ZipFile zf = new ZipFile(zipFile);

FileOutputStream fos;

System.out.println("正在解压文件..." + zipFile);

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();

}

System.out.println("成功解压文件: "
+ zipFile);

flag = true;

}

if(unfile=="rar"||unfile.equals("rar")){

unrarCmd += zipFile + " " + outFilePath;

try {

System.out.println("正在解压文件..." + zipFile);

Runtime rt = Runtime.getRuntime();

Process p = rt.exec(unrarCmd);

System.out.println("成功解压文件: "
+ zipFile);

flag=true;

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}catch(Exception e){

e.printStackTrace();

}

return flag;

}

public static void main(String[] args)

{

try

{

Zipper zip=new Zipper();

String zipFile="F:\\新建文件夹.zip";

String outFilePath="F:\\";

int mode=1;

//测试解压

zip.unfile(zipFile, outFilePath, mode);

//测试压缩

// if(Zipper.createZip("F:\\新建文件夹.rar",aa)){

// System.out.println("成功压缩");

// }

}

catch (Exception e)

{

e.printStackTrace();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: