- 浏览: 275156 次
- 性别:
- 来自: 广州
文章分类
private void unzip() { int BUFFER = 2048; try { String fileName = "C:/img1.zip"; String filePath = "C:/"; ZipFile zipFile = new ZipFile(fileName); Enumeration emu = zipFile.getEntries(); while (emu.hasMoreElements()) { ZipEntry entry = (ZipEntry) emu.nextElement(); // 会把目录作为一个file读出一次,所以只建立目录就可以,之下的文件还会被迭代到。 if (entry.isDirectory()) { new File(filePath + entry.getName()).mkdirs(); continue; } BufferedInputStream bis = new BufferedInputStream( zipFile.getInputStream(entry)); File file = new File(filePath + entry.getName()); // 加入这个的原因是zipfile读取文件是随机读取的,这就造成可能先读取一个文件 // 而这个文件所在的目录还没有出现过,所以要建出目录来。 File parent = file.getParentFile(); if (parent != null && (!parent.exists())) { parent.mkdirs(); } FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); bos.close(); bis.close(); } zipFile.close(); } catch (Exception e) { e.printStackTrace(); } }
从 http://ant.apache.org/bindownload.cgi 下载apache-ant-1.8.2-bin.zip,将ant.jar加到编译路径,然后:
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
mport java.io.*;
import org.apache.tools.zip.*;
import java.util.Enumeration;
/**
*功能:zip压缩、解压(支持中文文件名)
*说明:本程序通过使用Apache Ant里提供的zip工具org.apache.tools.zip实现了zip压缩和解压功能.
* 解决了由于java.util.zip包不支持汉字的问题。
* 使用java.util.zip包时,当zip文件中有名字为中文的文件时,
* 就会出现异常:"Exception in thread "main " java.lang.IllegalArgumentException
* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
*注意:
* 1、使用时把ant.jar放到classpath中,程序中使用import org.apache.tools.zip.*;
* 2、Apache Ant 下载地址:[url]http://ant.apache.org/[/url]
* 3、Ant ZIP API:[url]http://www.jajakarta.org/ant/ant-1.6.1/docs/mix/manual/api/org/apache/tools/zip/[/url]
* 4、本程序使用Ant 1.7.1 中的ant.jar
*
*仅供编程学习参考.
*
*@author Winty
*@date 2008-8-3
*@Usage:
* 压缩:java AntZip -zip "directoryName"
* 解压:java AntZip -unzip "fileName.zip"
*/
public class AntZip{
private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes;
public AntZip(){
this(512);
}
public AntZip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
//压缩文件夹内的文件
public void doZip(String zipDirectory){//zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
//由doZip调用,递归完成目录文件读取
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){//如果目录为空,则单独创建之.
//ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{//如果目录不为空,则分别处理目录和文件.
for(File fileName : files){
//System.out.println(fileName);
if(fileName.isDirectory()){
handleDir(fileName , this.zipOut);
}
else{
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while((this.readedBytes = fileIn.read(this.buf))>0){
this.zipOut.write(this.buf , 0 , this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
}
//解压指定zip文件
public void unZip(String unZipfileName){//unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
InputStream inputStream;
try{
this.zipFile = new ZipFile(unZipfileName);
for(Enumeration entries = this.zipFile.getEntries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
file = new File(entry.getName());
if(entry.isDirectory()){
file.mkdirs();
}
else{
//如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if(!parent.exists()){
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
while(( this.readedBytes = inputStream.read(this.buf) ) > 0){
fileOut.write(this.buf , 0 , this.readedBytes );
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
//设置缓冲区大小
public void setBufSize(int bufSize){
this.bufSize = bufSize;
}
//测试AntZip类
public static void main(String[] args)throws Exception{
if(args.length==2){
String name = args[1];
AntZip zip = new AntZip();
if(args[0].equals("-zip"))
zip.doZip(name);
else if(args[0].equals("-unzip"))
zip.unZip(name);
}
else{
System.out.println("Usage:");
System.out.println("压缩:java AntZip -zip directoryName");
System.out.println("解压:java AntZip -unzip fileName.zip");
throw new Exception("Arguments error!");
}
}
}
import org.apache.tools.zip.*;
import java.util.Enumeration;
/**
*功能:zip压缩、解压(支持中文文件名)
*说明:本程序通过使用Apache Ant里提供的zip工具org.apache.tools.zip实现了zip压缩和解压功能.
* 解决了由于java.util.zip包不支持汉字的问题。
* 使用java.util.zip包时,当zip文件中有名字为中文的文件时,
* 就会出现异常:"Exception in thread "main " java.lang.IllegalArgumentException
* at java.util.zip.ZipInputStream.getUTF8String(ZipInputStream.java:285)
*注意:
* 1、使用时把ant.jar放到classpath中,程序中使用import org.apache.tools.zip.*;
* 2、Apache Ant 下载地址:[url]http://ant.apache.org/[/url]
* 3、Ant ZIP API:[url]http://www.jajakarta.org/ant/ant-1.6.1/docs/mix/manual/api/org/apache/tools/zip/[/url]
* 4、本程序使用Ant 1.7.1 中的ant.jar
*
*仅供编程学习参考.
*
*@author Winty
*@date 2008-8-3
*@Usage:
* 压缩:java AntZip -zip "directoryName"
* 解压:java AntZip -unzip "fileName.zip"
*/
public class AntZip{
private ZipFile zipFile;
private ZipOutputStream zipOut; //压缩Zip
private ZipEntry zipEntry;
private static int bufSize; //size of bytes
private byte[] buf;
private int readedBytes;
public AntZip(){
this(512);
}
public AntZip(int bufSize){
this.bufSize = bufSize;
this.buf = new byte[this.bufSize];
}
//压缩文件夹内的文件
public void doZip(String zipDirectory){//zipDirectoryPath:需要压缩的文件夹名
File file;
File zipDir;
zipDir = new File(zipDirectory);
String zipFileName = zipDir.getName() + ".zip";//压缩后生成的zip文件名
try{
this.zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
handleDir(zipDir , this.zipOut);
this.zipOut.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
//由doZip调用,递归完成目录文件读取
private void handleDir(File dir , ZipOutputStream zipOut)throws IOException{
FileInputStream fileIn;
File[] files;
files = dir.listFiles();
if(files.length == 0){//如果目录为空,则单独创建之.
//ZipEntry的isDirectory()方法中,目录以"/"结尾.
this.zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
this.zipOut.closeEntry();
}
else{//如果目录不为空,则分别处理目录和文件.
for(File fileName : files){
//System.out.println(fileName);
if(fileName.isDirectory()){
handleDir(fileName , this.zipOut);
}
else{
fileIn = new FileInputStream(fileName);
this.zipOut.putNextEntry(new ZipEntry(fileName.toString()));
while((this.readedBytes = fileIn.read(this.buf))>0){
this.zipOut.write(this.buf , 0 , this.readedBytes);
}
this.zipOut.closeEntry();
}
}
}
}
//解压指定zip文件
public void unZip(String unZipfileName){//unZipfileName需要解压的zip文件名
FileOutputStream fileOut;
File file;
InputStream inputStream;
try{
this.zipFile = new ZipFile(unZipfileName);
for(Enumeration entries = this.zipFile.getEntries(); entries.hasMoreElements();){
ZipEntry entry = (ZipEntry)entries.nextElement();
file = new File(entry.getName());
if(entry.isDirectory()){
file.mkdirs();
}
else{
//如果指定文件的目录不存在,则创建之.
File parent = file.getParentFile();
if(!parent.exists()){
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(file);
while(( this.readedBytes = inputStream.read(this.buf) ) > 0){
fileOut.write(this.buf , 0 , this.readedBytes );
}
fileOut.close();
inputStream.close();
}
}
this.zipFile.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
//设置缓冲区大小
public void setBufSize(int bufSize){
this.bufSize = bufSize;
}
//测试AntZip类
public static void main(String[] args)throws Exception{
if(args.length==2){
String name = args[1];
AntZip zip = new AntZip();
if(args[0].equals("-zip"))
zip.doZip(name);
else if(args[0].equals("-unzip"))
zip.unZip(name);
}
else{
System.out.println("Usage:");
System.out.println("压缩:java AntZip -zip directoryName");
System.out.println("解压:java AntZip -unzip fileName.zip");
throw new Exception("Arguments error!");
}
}
}
发表评论
-
java IP地址转换
2013-03-28 09:05 984/**将给定的字节数组转换成IPV4的十进制分段表示格式的ip ... -
ScreenCapture
2012-08-02 11:59 935import java.awt.Rectangle; impo ... -
UdpSpeedTest
2012-06-04 18:26 944import java.net.DatagramPacket ... -
PieChart3DDemo3.java
2012-04-24 13:41 1046import java.awt.BorderLayout ... -
java modbus
2012-04-06 13:54 1123modbus -
16款Java图表组件
2012-04-06 13:50 2373开源Java图表组件 1. JFreeChar ... -
java md5
2012-02-17 14:29 776import java.security.MessageDig ... -
FreeModbus
2012-02-14 11:15 811http://freemodbus.berlios.de/ap ... -
java 获取当前日期与时间
2012-01-19 16:52 847time = new SimpleDateFormat(&q ... -
java string sort
2012-01-19 16:46 819Arrays.sort(filelist, new Co ... -
java run dos command
2012-01-19 16:44 675Runtime.getRuntime().exec(" ... -
eclipse plugs
2012-01-17 16:47 693http://checkthread.org/eclipse- ... -
java
2012-01-12 17:18 797BufferedWriter out = new Buf ... -
Jpcap
2012-01-04 19:36 937Jpcap is a Java library for ... -
java 获取网卡信息
2011-12-21 15:25 1107public static void main(Stri ... -
java 多网卡绑定例程
2011-12-21 15:01 1555单播: DatagramSocket s = n ... -
java multicast demo
2011-12-20 08:46 829import java.net.DatagramPack ... -
JavaService把java程序发布为windows服务
2011-12-09 14:45 872http://forge.ow2.org/projects/j ... -
chilkatsoft
2011-11-30 17:23 850http://www.chilkatsoft.com/ -
csv
2011-11-30 16:40 863Java CSV is a small fast open s ...
相关推荐
java zip压缩文件 加密代码和jar包
解决java zip压缩文件里面带有中文文件,在java代码解压时报错,或者无法解压问题
网上很多描述java解压中文乱码的问题,很多描述不全.由于工作需要整理出一个完整版.简单实用.下载后请从ZipUtil.java的main方法开始,一目了然. public static void main(String args[]) { new ZipUtil().unZip("E:\\...
java zip压缩和解压缩文件,附带ant.jar
java zip文件的压缩与解压缩,支持中文 使用apache的工具包可设置字符编码
解决java ZIP压缩、解压缩(同时加解密)问题,代码部分
import org.apache.commons.compress.archivers.zip.Zip64Mode; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream; ...
使用java的zip压缩库 对文件和文件夹实现压缩
2000年左右用java作的仿winzip的小工源码 zz1976@163.com
利用zip进行数据压缩
java 操作 zip压缩,解压缩 java 操作 zip压缩,解压缩 java 操作 zip压缩,解压缩 java 操作 zip压缩,解压缩 java 操作 zip压缩,解压缩
java zip 压缩解压例子
Java zip压缩包查看程序源码.zip
javaZIP压缩源代码,可以很方便的进行打包.源码含有详细的注释.
Android Java zip 解压库,全部源代码,代码很精简,可以取到每个文件在压缩文件中的起始位置及大小信息。
zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java zip 压缩 文件 Java
java zip xls 相关文档
Java zip解压,并遍历zip中的配置文件 .cfg或.properties,项目实用
主要介绍了基于Java向zip压缩包追加文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java zip压缩包查看程序源码