java zip递归压缩解压代码
来源:互联网 发布:北京奥运会赔钱知乎 编辑:程序博客网 时间:2024/06/01 23:35
ZIP压缩类
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipCompress ...{
/** *//**
* @param args
*/
public static void main(String[] args) throws IOException ...{
compress("D:/tomcat-5.5.20","d:/test/testZip.zip");
}
/** *//**
* 递归压缩文件
* @param source 源路径,可以是文件,也可以目录
* @param destinct 目标路径,压缩文件名
* @throws IOException
*/
private static void compress(String source,String destinct) throws IOException ...{
List fileList=loadFilename(new File(source));
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(destinct)));
byte[] buffere=new byte[8192];
int length;
BufferedInputStream bis;
for(int i=0;i<fileList.size();i++) ...{
File file=(File) fileList.get(i);
zos.putNextEntry(new ZipEntry(getEntryName(source,file)));
bis=new BufferedInputStream(new FileInputStream(file));
while(true) ...{
length=bis.read(buffere);
if(length==-1) break;
zos.write(buffere,0,length);
}
bis.close();
zos.closeEntry();
}
zos.close();
}
/** *//**
* 递归获得该文件下所有文件名(不包括目录名)
* @param file
* @return
*/
private static List loadFilename(File file) ...{
List filenameList=new ArrayList();
if(file.isFile()) ...{
filenameList.add(file);
}
if(file.isDirectory()) ...{
for(File f:file.listFiles()) ...{
filenameList.addAll(loadFilename(f));
}
}
return filenameList;
}
/** *//**
* 获得zip entry 字符串
* @param base
* @param file
* @return
*/
private static String getEntryName(String base,File file) ...{
File baseFile=new File(base);
String filename=file.getPath();
//int index=filename.lastIndexOf(baseFile.getName());
if(baseFile.getParentFile().getParentFile()==null)
return filename.substring(baseFile.getParent().length());
return filename.substring(baseFile.getParent().length()+1);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipCompress ...{
/** *//**
* @param args
*/
public static void main(String[] args) throws IOException ...{
compress("D:/tomcat-5.5.20","d:/test/testZip.zip");
}
/** *//**
* 递归压缩文件
* @param source 源路径,可以是文件,也可以目录
* @param destinct 目标路径,压缩文件名
* @throws IOException
*/
private static void compress(String source,String destinct) throws IOException ...{
List fileList=loadFilename(new File(source));
ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(new File(destinct)));
byte[] buffere=new byte[8192];
int length;
BufferedInputStream bis;
for(int i=0;i<fileList.size();i++) ...{
File file=(File) fileList.get(i);
zos.putNextEntry(new ZipEntry(getEntryName(source,file)));
bis=new BufferedInputStream(new FileInputStream(file));
while(true) ...{
length=bis.read(buffere);
if(length==-1) break;
zos.write(buffere,0,length);
}
bis.close();
zos.closeEntry();
}
zos.close();
}
/** *//**
* 递归获得该文件下所有文件名(不包括目录名)
* @param file
* @return
*/
private static List loadFilename(File file) ...{
List filenameList=new ArrayList();
if(file.isFile()) ...{
filenameList.add(file);
}
if(file.isDirectory()) ...{
for(File f:file.listFiles()) ...{
filenameList.addAll(loadFilename(f));
}
}
return filenameList;
}
/** *//**
* 获得zip entry 字符串
* @param base
* @param file
* @return
*/
private static String getEntryName(String base,File file) ...{
File baseFile=new File(base);
String filename=file.getPath();
//int index=filename.lastIndexOf(baseFile.getName());
if(baseFile.getParentFile().getParentFile()==null)
return filename.substring(baseFile.getParent().length());
return filename.substring(baseFile.getParent().length()+1);
}
}
ZIP解压类
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipDecompression ...{
/** *//**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException ...{
decompression("d:/test/testZip.zip","d:/test/un");
}
/** *//**
* 解压ZIP文件
* @param zipFile 要解压的ZIP文件路径
* @param destination 解压到哪里
* @throws IOException
*/
public static void decompression(String zipFile,String destination) throws IOException ...{
ZipFile zip=new ZipFile(zipFile);
Enumeration en=zip.entries();
ZipEntry entry=null;
byte[] buffer=new byte[8192];
int length=-1;
InputStream input=null;
BufferedOutputStream bos=null;
File file=null;
while(en.hasMoreElements()) ...{
entry=(ZipEntry)en.nextElement();
if(entry.isDirectory()) ...{
System.out.println("directory");
continue;
}
input=zip.getInputStream(entry);
file=new File(destination,entry.getName());
if(!file.getParentFile().exists()) ...{
file.getParentFile().mkdirs();
}
bos=new BufferedOutputStream(new FileOutputStream(file));
while(true) ...{
length=input.read(buffer);
if(length==-1) break;
bos.write(buffer,0,length);
}
bos.close();
input.close();
}
zip.close();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipDecompression ...{
/** *//**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException ...{
decompression("d:/test/testZip.zip","d:/test/un");
}
/** *//**
* 解压ZIP文件
* @param zipFile 要解压的ZIP文件路径
* @param destination 解压到哪里
* @throws IOException
*/
public static void decompression(String zipFile,String destination) throws IOException ...{
ZipFile zip=new ZipFile(zipFile);
Enumeration en=zip.entries();
ZipEntry entry=null;
byte[] buffer=new byte[8192];
int length=-1;
InputStream input=null;
BufferedOutputStream bos=null;
File file=null;
while(en.hasMoreElements()) ...{
entry=(ZipEntry)en.nextElement();
if(entry.isDirectory()) ...{
System.out.println("directory");
continue;
}
input=zip.getInputStream(entry);
file=new File(destination,entry.getName());
if(!file.getParentFile().exists()) ...{
file.getParentFile().mkdirs();
}
bos=new BufferedOutputStream(new FileOutputStream(file));
while(true) ...{
length=input.read(buffer);
if(length==-1) break;
bos.write(buffer,0,length);
}
bos.close();
input.close();
}
zip.close();
}
}
- java zip递归压缩解压代码
- JAVA压缩/解压ZIP
- Java压缩/解压ZIP
- java zip压缩解压
- java,zip压缩,解压。
- Java Zip 压缩、解压
- java实现Zip文件压缩和解压工程代码
- 用java代码实现文件的zip压缩与解压
- 解压zip压缩包(JAVA)
- java压缩和解压zip
- java解压、压缩、操作ZIP
- Java ZIp 压缩和解压
- java解压Zip压缩包
- java zip 压缩与解压
- java算法 -- zip解压,压缩
- java zip压缩和解压
- java解压zip压缩包
- java实现zip压缩、解压
- VC中的string table(
- .net中的WMI编程(一):WMI介绍及简单应用
- 跨平台的端口扫描工具:nmap
- 关于Linux中动态库的一些学习
- 水晶报表中设置报表设计宽度
- java zip递归压缩解压代码
- sqlserver 跨数据库 访问
- 2007年企业级Web 2.0技术的十件大事
- ExtJS与.NET结合开发实例(Grid之数据显示、分页、排序篇)
- C#转换AVI文件为BMP文件
- 16色的“诱惑”
- 实时卫星影像与空中监视
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 使用VC调用matlab engine编程