file压缩

来源:互联网 发布:详情页设计软件 编辑:程序博客网 时间:2024/05/22 22:32
package com.ziping;

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 java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;


public class UnZip {

    public static void main(String[] args){
        zip(new File("e:/crmo.sql"),new File("c:/"+System.currentTimeMillis()+".zip"));
        
    }
    public static void unZipFile(File zipPath,File unZipPath)
    {
            ZipFile zipFile=null;
             InputStream input = null;
             OutputStream output = null;
         
                try {
                 zipFile=new ZipFile(zipPath);             
                Enumeration<?> entries = zipFile.entries();
                while(entries.hasMoreElements())
                {
                    ZipEntry zipEntry = (ZipEntry)entries.nextElement();
                  if(zipEntry.isDirectory())
                    {
                      String nameString = zipEntry.getName();
                      System.out.println(nameString);
                      nameString = nameString.substring(0,nameString.length()-1);
                      unZipPath.mkdirs();
                    }
                    else
                    {
                       String filePath = unZipPath.getAbsolutePath()+File.separator+zipEntry.getName();
                       System.out.println(zipEntry.getName());
                       filePath = new String(filePath.getBytes(),"gbk");
                       File file = new File(filePath);
                       File parentFile= file.getParentFile();
                       if(!parentFile.exists()) {
                          parentFile.mkdirs();
                       }
                       input = zipFile.getInputStream(zipEntry);
                       output = new FileOutputStream(file);
                       byte[] buffer = new byte[8092];
                       int realLength = 0;
                       while((realLength = input.read(buffer)) != -1)
                       {
                             output.write(buffer,0,realLength);
                       }
                   }
                }
                    zipFile.close();
                    output.close();
                    input.close();
                } catch (ZipException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                 }
             
    }
     public static void zip(File sourceFolder, File zipFile) {
         ZipOutputStream zos = null;
         try {
             ZipOutputStream zos2 = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
           
                 zipFile(sourceFolder, sourceFolder.isDirectory() ? sourceFolder.getPath() : sourceFolder.getParent(), zos2);
                 zos2.closeEntry();
                 zos2.close();
                } catch (Exception e2) {
                  e2.printStackTrace();
             }     
         
   }

   public static void zip(String sourceDir, String zipFile) {
       zip(new File(sourceDir), new File(zipFile));
   }

   private static void zipFile(File source, String basePath, ZipOutputStream zos) {
       // 创建文件对象用于装载要压缩文件  
       File[] files = new File[0];  
 
       String pathName;  
 
       int length = 0;  
       // 检测是否有子目录  
       if (source.isDirectory()) {  
           // 获取目录下所有文档  
           files = source.listFiles();  
       } else {  
           files = new File[1];  
           files[0] = source;  
       }  
       byte[] buf = new byte[1024];  
       try {  
           for (File file : files) {  
               if (file.isDirectory()) {  
                   // 获得文件路径+“/”表示还有子目录  
                   pathName = file.getPath().substring(basePath.length())  
                           + "/";  
                   // 创建一个Zip文件目录进行传递给输出流  
                   zos.putNextEntry(new ZipEntry(pathName));  
                   // 递归压缩到没有子目录  
                  zipFile(file, basePath, zos);  
               } else {  
 
                   pathName = file.getPath().substring(basePath.length());  
                   // 打开文件输入通道  
                   InputStream is = new FileInputStream(file);  
                   // 使用缓冲流对接压缩流  
                   BufferedInputStream bis = new BufferedInputStream(is);  
                   // 创建压缩目录  
                   zos.putNextEntry(new ZipEntry(pathName));  
                   // 循环写入压缩流  
                   while ((length = bis.read(buf)) > 0) {  
                       zos.write(buf, 0, length);  
                   }  
                   // 关闭文件流  
                   is.close();  
               }  
           }  
       } catch (Exception e) {  
           // TODO: handle exception  
       }  
 
       
   }
}    
         

0 0
原创粉丝点击