java实现压缩解压

package com.cn.love.zip;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;


import org.apache.commons.lang3.StringUtils; 
public class javazip {
/**
* 递归压缩文件夹 
* @param srcRootDir压缩文件夹根目录的子路径 
* @param file 当前递归压缩的文件或目录对象 
* @param zos 当前递归压缩的文件或目录对象 
* @throws Exception 异常
*/
private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception  
   {  
       if (file == null)   
       {  
           return;  
       }                 
         
       //如果是文件,则直接压缩该文件  
       if (file.isFile())  
       {             
           int count, bufferLen = 1024;  
           byte data[] = new byte[bufferLen];  
             
           //获取文件相对于压缩文件夹根目录的子路径  
           String subPath = file.getAbsolutePath();  
           int index = subPath.indexOf(srcRootDir);  
           if (index != -1)   
           {  
               subPath = subPath.substring(srcRootDir.length() + File.separator.length());  
           }  
           ZipEntry entry = new ZipEntry(subPath);  
           zos.putNextEntry(entry);  
           BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));  
           while ((count = bis.read(data, 0, bufferLen)) != -1)   
           {  
               zos.write(data, 0, count);  
           }  
           bis.close();  
           zos.closeEntry();  
       }  
       //如果是目录,则压缩整个目录  
       else   
       {  
           //压缩目录中的文件或子目录  
           File[] childFileList = file.listFiles();  
           for (int n=0; n entries = (Enumeration)zip.entries();  
       //循环对压缩包里的每一个文件进行解压       
       while(entries.hasMoreElements())  
       {  
           entry = entries.nextElement();  
           //构建压缩包中一个文件解压后保存的文件全路径  
           entryFilePath = unzipFilePath + File.separator + entry.getName();  
           //构建解压后保存的文件夹路径  
           index = entryFilePath.lastIndexOf(File.separator);  
           if (index != -1)  
           {  
               entryDirPath = entryFilePath.substring(0, index);  
           }  
           else  
           {  
               entryDirPath = "";  
           }             
           entryDir = new File(entryDirPath);  
           //如果文件夹路径不存在,则创建文件夹  
           if (!entryDir.exists() || !entryDir.isDirectory())  
           {  
               entryDir.mkdirs();  
           }  
             
           //创建解压文件  
           entryFile = new File(entryFilePath);  
           if (entryFile.exists())  
           {  
               //检测文件是否允许删除,如果不允许删除,将会抛出SecurityException  
               SecurityManager securityManager = new SecurityManager();  
               securityManager.checkDelete(entryFilePath);  
               //删除已存在的目标文件  
               entryFile.delete();   
           }  
             
           //写入文件  
           bos = new BufferedOutputStream(new FileOutputStream(entryFile));  
           bis = new BufferedInputStream(zip.getInputStream(entry));  
           while ((count = bis.read(buffer, 0, bufferSize)) != -1)  
           {  
               bos.write(buffer, 0, count);  
           }  
           bos.flush();  
           bos.close();              
       }  
   }  
   public static void print(File f){
       if(f!=null){
           if(f.isDirectory()){
               File[] fileArray=f.listFiles();
               if(fileArray!=null){
                   for (int i = 0; i < fileArray.length; i++) {
                       //递归调用
                       print(fileArray[i]);
                   }
               }
           }
           else{
               System.out.println(f);
           }
       }
   }
public static void main(String[] args) {
//压缩
String zipPath = "C:\\Users\\software\\Desktop"; //压缩文件保存的路径
        String dir = "C:\\Users\\software\\Desktop\\array";   //要压缩的源文件路径
        String zipFileName = "test.zip";   //压缩文件名 
        try  
        {  
            zip(dir, zipPath, zipFileName);  
        }   
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        //解压
        String zipFilePath = "C:\\Users\\software\\Desktop\\test.zip";//zip文件的全路径   
        String unzipFilePath = "C:\\Users\\software\\Desktop"; // 解压后的文件保存的路径 
        try   
        {  
            unzip(zipFilePath, unzipFilePath, true);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        } 
        String fileName=unzipFilePath+"\\test";
        File f=new File(fileName);
        print(f);
}
}


你可能感兴趣的:(java)