DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

java实现zip与unzip

jdk提供了Zip相关的类方便的实现压缩和解压缩。使用方法很简单。下边分别是压缩和解压缩的简单事例
1,压缩的
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip {
    
static final int BUFFER = 2048;

    
public static void main(String argv[]) {
        
try {
            BufferedInputStream origin 
= null;
            FileOutputStream dest 
= new FileOutputStream("E:\\test\\myfiles.zip");
            ZipOutputStream out 
= new ZipOutputStream(new BufferedOutputStream(
                    dest));
            
byte data[] = new byte[BUFFER];
            File f 
= new File("e:\\test\\a\\");
            File files[] 
= f.listFiles();

            
for (int i = 0; i < files.length; i++{
                FileInputStream fi 
= new FileInputStream(files[i]);
                origin 
= new BufferedInputStream(fi, BUFFER);
                ZipEntry entry 
= new ZipEntry(files[i].getName());
                out.putNextEntry(entry);
                
int count;
                
while ((count = origin.read(data, 0, BUFFER)) != -1{
                    out.write(data, 
0, count);
                }

                origin.close();
            }

            out.close();
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

}


2,解压缩的。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UnZip {
    
static final int BUFFER = 2048;

    
public static void main(String argv[]) {
        
try {
            String fileName 
= "E:\\test\\myfiles.zip";
            String filePath 
= "E:\\test\\";
            ZipFile zipFile 
= new ZipFile(fileName);
            Enumeration emu 
= zipFile.entries();
            
int i=0;
            
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();
        }

    }

}

posted on 2007-08-09 09:33 dreamstone 阅读(20611) 评论(8)  编辑  收藏 所属分类: jdk相关

评论

# re: java实现zip与unzip 2007-08-10 14:32 wpf

你这个应该是不支持中文的吧  回复  更多评论   

# re: java实现zip与unzip 2007-08-10 15:28 dreamstone

这个只是个简单的demo,想支持中文,变通一下就可以  回复  更多评论   

# re: java实现zip与unzip 2007-09-18 16:52 yondchang

多谢多谢
PS:

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
改成
import org.apache.tools.zip.*;

然后把
Enumeration emu = zipFile.entries();
改成
Enumeration emu = zipFile.getEntries();
就可以支持中文了  回复  更多评论   

# re: java实现zip与unzip 2007-11-07 17:09 siyuan

楼主可以发个包给我吗?
org.apache.tools包在网上没有找到
我的邮箱是592262029@qq.com  回复  更多评论   

# re: java实现zip与unzip 2008-03-27 23:43 fenixshadow

完全使用ant会更简单:
static public void unzip(String zipFilepath, String destinationDir) {

final class Expander extends Expand {
public Expander() {
project = new Project();
project.init();
taskType = "unzip";
taskName = "unzip";
target = new Target();
}
}
Expander expander = new Expander();
expander.setSrc(new File(zipfile));
expander.setDest(new File(destdir));
expander.execute();
}  回复  更多评论   

# re: java实现zip与unzip 2008-08-04 17:41 Shit

收藏了  回复  更多评论   

# re: java实现zip与unzip 2010-09-07 11:10 Ring~Lee

第一个压缩方法,只支持e:\\test\\a\\下全是文件的场景。
如果e:\\test\\a\\下还有子文件夹,就不支持了。  回复  更多评论   

# re: java实现zip与unzip[未登录] 2012-08-27 11:32 贝贝

很好。。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: