`
ljs120ljs
  • 浏览: 7498 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

java 对文件或文件夹的压缩与解压(ant.jar)

阅读更多
package selftest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

/**
* 文件压缩和解压
* @author newuser
*用java.util.zip下的工具压缩
*用ant.jar包里面的工具压缩
*/
public class Zip
{
private int count;


public int getCount()
{
return count;
}

public void setCount(int count)
{
this.count = count;
}
/**
* @param args
*/
public static void main(String[] args)
{
try
{

Zip zip = new Zip();
zip.toFile("D:\\apache-ant-1.7.1.zip", "D:\\apache");
//zip.unzip("D:\\apache-ant-1.7.1.zip", "D:\\apache");

System.out.println("ok");

}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void unzip(String zipFileName, String extPlace) throws Exception {  
        try {  
            (new File(extPlace)).mkdirs();  
            File f = new File(zipFileName);  
            ZipFile zipFile = new ZipFile(zipFileName);  
            if((!f.exists()) && (f.length() <= 0)) {  
                throw new Exception("要解压的文件不存在!");  
            }  
            String strPath, gbkPath, strtemp;  
            File tempFile = new File(extPlace);  
            strPath = tempFile.getAbsolutePath();  
            java.util.Enumeration e = zipFile.getEntries();  
            while(e.hasMoreElements()){  
                org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();  
                gbkPath=zipEnt.getName();  
                if(zipEnt.isDirectory()){  
                    strtemp = strPath + File.separator + gbkPath;  
                    File dir = new File(strtemp);  
                    dir.mkdirs();  
                    continue;  
                } else {  
                    //读写文件  
                    InputStream is = zipFile.getInputStream(zipEnt);  
                    BufferedInputStream bis = new BufferedInputStream(is);  
                    gbkPath=zipEnt.getName();  
                    strtemp = strPath + File.separator + gbkPath;  
                  
                    //建目录  
                    String strsubdir = gbkPath;  
                    for(int i = 0; i < strsubdir.length(); i++) {  
                        if(strsubdir.substring(i, i + 1).equalsIgnoreCase(File.separator)) {  
                            String temp = strPath + File.separator + strsubdir.substring(0, i);  
                            File subdir = new File(temp);  
                            if(!subdir.exists())  
                            subdir.mkdir();  
                        }  
                    }  
                    FileOutputStream fos = new FileOutputStream(strtemp);  
                    BufferedOutputStream bos = new BufferedOutputStream(fos);  
                    int c;  
                    while((c = bis.read()) != -1) {  
                        bos.write((byte) c);  
                    }  
                    bos.close();  
                    fos.close();  
                }  
            }  
        } catch(Exception e) {  
            e.printStackTrace();  
            throw e;  
        }  
    }  




/**
* 递归压缩文件
* @param path 被压缩的文件或文件夹
* @param zos  压缩流 ant.jar
*/
public void toZip(String path, String basePath, ZipOutputStream zos, Zip zip)
{
try
{
File srcFile = new File(path);
if (srcFile.exists())
{
File[] files = null;
if (srcFile.isDirectory())//目录
{
files = srcFile.listFiles();//此目录中的所有文件或文件夹
}
else if (srcFile.isFile())//文件
{
files = new File[1];
files[0] = srcFile;
}
//开始压缩
if (files != null)
{
File tempFile = null;
String tempFileName = null;

for (int i = 0; i < files.length; i++)
{
tempFile = files[i];
/*
* C:\test
* C:\test\bin
* C:\test\ttt\test.txt
*/
zip.setCount(zip.getCount()+1);
tempFileName = tempFile.getPath().replace(basePath+File.separator, "");
if (tempFile.isDirectory())
{  
//tempFileName = tempFile.getPath().substring(srcFile.getPath().length()+1);
zos.putNextEntry(new ZipEntry(tempFileName+"/"));
toZip(tempFile.getPath(),basePath, zos, zip);
}
else if(tempFile.isFile())
{
//tempFileName = tempFile.getPath().substring(srcFile.getParent().length()+1);
zos.putNextEntry(new ZipEntry(tempFileName));
//DataInputStream dis = new DataInputStream(new FileInputStream(tempFile));
FileInputStream fin = new FileInputStream(files[i]);
              
byte[] b = new byte[1048];
int len = 0;
while ((len = fin.read(b)) > 0)
{
zos.write(b, 0, len);
}
fin.close();
}

System.out.println(tempFileName);
}
}
}
else
{
System.out.println("文件或文件夹不存在!");
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
  • ant.jar (1.4 MB)
  • 下载次数: 53
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics