java 压缩目录为 zip

      用java好久了,还没有写个压缩文件的示例,昨晚弄了下,把写下来,以后可以看。

关系到
java.util.zip.ZipEntry
java.util.zip.ZipOutputStream

如果要解决中文文件名问题,用到ant.jar

这两个类。

ZipOutputStream.putNextEntry(ZipEntry);就可以了,然后ZipOutputStream.wirte();就得了。

package  net.blogjava.chenlb.zip;

import  java.io.BufferedOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.OutputStream;
// import java.util.zip.ZipEntry;
// import java.util.zip.ZipOutputStream;
// 用ant.jar的zip.*可以解决中文文件名问题
import  org.apache.tools.zip.ZipEntry;
import  org.apache.tools.zip.ZipOutputStream;

/**
 * 压缩文件.
 * 2007-10-17 下午11:19:50
 * 
@author  chenlb
 
*/
public   class  RecursiveZip {

    
    
public   static   void  main(String[] args) {

        RecursiveZip recursiveZip 
=   new  RecursiveZip();
        System.out.println(
" ====开始==== " );
        
try  {
            OutputStream os 
=   new  FileOutputStream( " e:/doc-recursive.zip " );
            BufferedOutputStream bs 
=   new  BufferedOutputStream(os);
            ZipOutputStream zo 
=   new  ZipOutputStream(bs);
            
            
// recursiveZip.zip("e:/recursive-zip/中文文件名.txt", new File("e:/recursive-zip"), zo, true, true);
            recursiveZip.zip( " e:/recursive-zip " new  File( " e:/recursive-zip " ), zo,  true true );
            
            zo.closeEntry();
            zo.close();
        } 
catch  (FileNotFoundException e) {
            e.printStackTrace();
        } 
catch  (IOException e) {
            e.printStackTrace();
        }
        System.out.println(
" ====完成==== " );
    }

    
/**
     * 
@param  path 要压缩的路径, 可以是目录, 也可以是文件.
     * 
@param  basePath 如果path是目录,它一般为new File(path), 作用是:使输出的zip文件以此目录为根目录, 如果为null它只压缩文件, 不解压目录.
     * 
@param  zo 压缩输出流
     * 
@param  isRecursive 是否递归
     * 
@param  isOutBlankDir 是否输出空目录, 要使输出空目录为true,同时baseFile不为null.
     * 
@throws  IOException
     
*/
    
public   void  zip(String path, File basePath, ZipOutputStream zo,  boolean  isRecursive,  boolean  isOutBlankDir)  throws  IOException {
        
        File inFile 
=   new  File(path);

        File[] files 
=   new  File[ 0 ];
        
if (inFile.isDirectory()) {     // 是目录
            files  =  inFile.listFiles();
        } 
else   if (inFile.isFile()) {     // 是文件
            files  =   new  File[ 1 ];
            files[
0 =  inFile;
        }
        
byte [] buf  =   new   byte [ 1024 ];
        
int  len;
        
// System.out.println("baseFile: "+baseFile.getPath());
         for ( int  i = 0 ; i < files.length; i ++ ) {
            String pathName 
=   "" ;
            
if (basePath  !=   null ) {
                
if (basePath.isDirectory()) {
                    pathName 
=  files[i].getPath().substring(basePath.getPath().length() + 1 );
                } 
else  { // 文件
                    pathName  =  files[i].getPath().substring(basePath.getParent().length() + 1 );
                }
            } 
else  {
                pathName 
=  files[i].getName();
            }
            System.out.println(pathName);
            
if (files[i].isDirectory()) {
                
if (isOutBlankDir  &&  basePath  !=   null ) {    
                    zo.putNextEntry(
new  ZipEntry(pathName + " / " ));     // 可以使空目录也放进去
                }
                
if (isRecursive) {     // 递归
                    zip(files[i].getPath(), basePath, zo, isRecursive, isOutBlankDir);
                }
            } 
else  {
                FileInputStream fin 
=   new  FileInputStream(files[i]);
                zo.putNextEntry(
new  ZipEntry(pathName));
                
while ((len = fin.read(buf)) > 0 ) {
                    zo.write(buf,
0 ,len);
                }
                fin.close();
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值