java zip API实现压缩和解压缩zip包

    package 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 ZipUtil {  
          
        /** 
         * 递归压缩文件夹 
         * @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 main(String[] args)   
        {  
            String zipPath = "d:\\ziptest\\zipPath";  
            String dir = "d:\\ziptest\\rawfiles";  
            String zipFileName = "test.zip";  
            try  
            {  
                zip(dir, zipPath, zipFileName);  
            }   
            catch (Exception e)  
            {  
                e.printStackTrace();  
            }  
              
            String zipFilePath = "D:\\ziptest\\zipPath\\test.zip";  
            String unzipFilePath = "D:\\ziptest\\zipPath";  
            try   
            {  
                unzip(zipFilePath, unzipFilePath, true);  
            }  
            catch (Exception e)  
            {  
                e.printStackTrace();  
            }  
        }  
    }  

转载于http://blog.csdn.net/stalwartwill/article/details/17373827

你可能感兴趣的:(java zip API实现压缩和解压缩zip包)