Java使用Zip包压缩文件示例
2012-10-12 11:27
801 查看
Java使用Zip包压缩文件示例
public static void zip() throws FileNotFoundException, IOException {File root = new File("svn-1.6");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(root.getPath()+".zip"));
zipDirectory(root,zipOutputStream);
zipOutputStream.close();
}
private static void zipDirectory(File root,ZipOutputStream zipOutputStream) throws IOException{
String zipPath = "";
zipDirectory(zipPath,root,zipOutputStream);
}
private static void zipDirectory(String basePath,File root,ZipOutputStream zipOutputStream) throws IOException{
if(basePath.length()>0){
basePath += "/";
}
File[] files = root.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if(file.isFile()){
System.out.println(file.getPath());
ZipEntry zipEntry = new ZipEntry(basePath + file.getName());
zipOutputStream.putNextEntry(zipEntry);
FileInputStream inputStream = new FileInputStream(file);
int count;byte[] buffer = new byte[1024];
while((count = inputStream.read(buffer, 0, buffer.length))>0){
zipOutputStream.write(buffer, 0, count);
}
inputStream.close();
}else if(file.isDirectory()){
ZipEntry zipEntry = new ZipEntry(basePath + file.getName()+"/");
zipOutputStream.putNextEntry(zipEntry);
zipDirectory(basePath + file.getName(),file,zipOutputStream);
}
}
}
相关文章推荐
- Java压缩文件工具类ZipUtil使用方法代码示例
- 使用java zip 压缩单个文件
- 使用java.util.zip压缩、解压文件
- 文件压缩java使用Apache的ant压缩和解压文件(zip)
- Java ZIP压缩文件使用总结
- java.util.zip包在解压缩文件方面入门使用
- 非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法
- Java文件压缩与解压之java.util.zip的使用
- 【Java】IO操作之使用zip包压缩和解压缩文件
- java 使用7-zip解压缩带密码的压缩文件(非Proccess方法)
- 使用java.util.zip包压缩和解压缩文件
- 使用Java zip包和apach 的ant包对文件、文件夹进行压缩,兼容中文
- Java 使用 org.apache.tools.zip.* 压缩文件源代码
- 使用java.util.zip包压缩和解压缩文件
- Java 使用 org.apache.tools.zip.* 压缩文件源代码
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- java使用Apache的ant压缩和解压文件(zip)
- 使用Python读写及压缩和解压缩文件的示例
- 使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!
- java使用ant快速压缩文件