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();
}
}
}
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();
}
}
}
相关文章推荐
- 【工作总结】LLDB调试技巧 - 篇一
- mysql linux下安装部署
- 发送SMS与MMS过程
- SMS-MMS的草稿箱
- NYOJ 会场安排问题
- 通过JDBC 连接MySQL问题
- JAVA开发环境配置
- 树结点,与度=边
- python ipython notebook教程
- 欢迎使用CSDN-markdown编辑器
- Ubuntu装完之后要……_(:з」∠)_
- 课后作业1
- [转载] Java字符串格式化
- SMS注册与接收短信
- POJ3630 Phone List 题解&代码
- ZOJ Paint the Grid Reloaded
- [HDU 2896] - 病毒侵袭 ac 自动机
- 一次多进程的小尝试
- hadoop搭建完毕后启动hadoop弹出ssh警告提示的解决办法
- ser2net和socat