Java压缩或解压文件实现

2018-07-20    来源:open-open

容器云强势上线!快速搭建集群,上万Linux镜像随意使用
    在处理压缩文件的时候,其实重点的就是递归的使用,区分压缩的是文件还是文件夹,并如何处理文件夹下的文件和文件夹。下面就直接把代码贴出来
     /**   
     *@Description: 压缩解压文件 
     */   
      
    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.ZipEntry;  
    import java.util.zip.ZipException;  
    import java.util.zip.ZipFile;  
    import java.util.zip.ZipOutputStream;  
      
    public class Zip {  
          
        private static int BUFFERSIZE = 32;//缓冲流大小  
        private static FileOutputStream fileOutputStream = null;  
        private static ZipOutputStream zipOutputStream = null;  
        private static byte[] array = new byte[BUFFERSIZE];  
          
        /** 
         * @param outputDir 
         * @param outputFileName 
         * @throws Exception 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:打开流 
         */  
        private static void openZipOutputStream(String outputDir, String outputFileName) throws Exception{  
            fileOutputStream = new FileOutputStream(outputDir + "\\" + outputFileName);  
            zipOutputStream = new ZipOutputStream(new BufferedOutputStream(fileOutputStream));  
        }  
          
        /** 
         * @throws Exception 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:关闭流 
         */  
        private static void closeZipOutputStream() throws Exception{  
            zipOutputStream.close();  
            fileOutputStream.close();  
        }  
          
        /** 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:缓存清空 
         */  
        private static void clearArrayBuffer(){  
            array = null;  
            array = new byte[BUFFERSIZE];  
        }  
          
        /** 
         * @param inputFile 
         * @param zipOutputStream 
         * @param base 
         * @param isPrint 
         * @throws Exception 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:压缩文件 
         */  
        private static void inputFileStream(File inputFile, ZipOutputStream zipOutputStream, String base,  boolean isPrint) throws Exception{  
            BufferedInputStream bufferedInputStream = null;  
            FileInputStream fileInputStream = new FileInputStream(inputFile);    
            bufferedInputStream = new BufferedInputStream(fileInputStream, BUFFERSIZE);    
            base = base.length() == 0 ? "" : base + "/";  
            if (isPrint) {  
                System.out.println(inputFile + "...");  
            }  
            ZipEntry zipEntry = new ZipEntry(base + inputFile.getName());  
            zipOutputStream.putNextEntry(zipEntry);  
      
            clearArrayBuffer();  
            while((bufferedInputStream.read(array, 0, BUFFERSIZE))!=-1){    
                zipOutputStream.write(array, 0, BUFFERSIZE);   
            }     
            bufferedInputStream.close();  
        }  
          
        /** 
         * @param inputFile 
         * @param zipOutputStream 
         * @param base 
         * @param isPrint 
         * @throws Exception 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:压缩文件夹 
         */  
        private static void inputDirectoryStream(File inputFile, ZipOutputStream zipOutputStream, String base,  boolean isPrint) throws Exception {  
            File[] files = inputFile.listFiles();  
            for (File file : files) {  
                if (file.isFile()) {  
                    //这里实现递归调用  
                    inputFileStream(file, zipOutputStream, base, isPrint);  
                } else {  
                    String baseN = (base.length() == 0 ? "" : base + "/") + file.getName();  
                    inputDirectoryStream(file, zipOutputStream, baseN, isPrint);  
                }  
            }  
        }  
          
        /** 
         * @param inputDir 
         * @param outputDir 
         * @param outputFileName 
         * @param isPrint 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description:压缩文件OR文件夹 
         */  
        private static void zip(String inputDir, String outputDir, String outputFileName, boolean isPrint) {  
            try {  
                File inputFile = new File(inputDir);  
                if (!inputFile.exists()) {  
                    System.out.println("Not found file " + inputDir);  
                    return;  
                }  
                File outputFile = new File(outputDir);  
                if (!outputFile.exists()) {  
                    outputFile.mkdirs();  
                }  
                  
                openZipOutputStream(outputDir, outputFileName);  
                  
                if (inputFile.isDirectory()) {  
                    inputDirectoryStream(inputFile, zipOutputStream, "", isPrint);  
                } else {  
                    inputFileStream(inputFile, zipOutputStream, "", isPrint);  
                }  
                  
                closeZipOutputStream();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
              
        }  
          
        /** 
         * @param inputFileName 
         * @param outputDir 
         * @param isPrint 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description: 解压文件 
         */  
        @SuppressWarnings("rawtypes")  
        private static void unZip(String inputFileName, String outputDir, boolean isPrint) {  
            try {  
                File inputFile = new File(inputFileName);  
                if (!inputFile.exists()) {  
                    System.out.println("Not found file " + inputFileName);  
                    return;  
                }  
                File outputFile = new File(outputDir);  
                if (!outputFile.exists()) {  
                    outputFile.mkdirs();  
                }  
                outputDir = outputDir.endsWith("[/|\\]") ? outputDir : outputDir + "/";  
                ZipFile zipFile = null;  
                try {  
                    zipFile = new ZipFile(inputFileName);   
                } catch (ZipException e) {  
                    System.out.println("File " + inputFileName + " isn't zip");  
                    return;  
                }  
                Enumeration enu = zipFile.entries();    
                while(enu.hasMoreElements()){    
                    ZipEntry zipEntry = (ZipEntry)enu.nextElement();    
                    if(zipEntry.isDirectory()){    
                        new File(outputDir+zipEntry.getName()).mkdirs();    
                        continue;    
                    }  
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));    
                    File file = new File(outputDir+zipEntry.getName());   
                    if (isPrint) {  
                        System.out.println(file + "...");  
                    }  
                    File parent = file.getParentFile();    
                    if(parent != null && !parent.exists()){    
                        parent.mkdirs();    
                    }    
                    FileOutputStream fileOutputStream = new FileOutputStream(file);    
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFERSIZE);    
                    clearArrayBuffer();   
                    while((bufferedInputStream.read(array, 0, BUFFERSIZE))!=-1){    
                        bufferedOutputStream.write(array, 0, BUFFERSIZE);   
                    }    
                        
                    bufferedOutputStream.flush();    
                    bufferedOutputStream.close();  
                    fileOutputStream.close();  
                    bufferedInputStream.close();    
                }   
                  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
              
        }  
          
        /** 
         * @param fileName 
         * @param isZip 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description: 解压OR压缩文件,不显示进度 
         */  
        public static void zipOrUnZip(String fileName, boolean isZip) {  
            zipOrUnZip(fileName, isZip, false);  
        }  
          
        /** 
         * @param fileName 
         * @param isZip 
         * @param isPrint 
         * @Date: 2013-8-8   
         * @Author: lulei   
         * @Description: 解压OR压缩文件,显示进度 
         */  
        public static void zipOrUnZip(String fileName, boolean isZip, boolean isPrint) {  
            File inputFile = new File(fileName);  
            if (!inputFile.exists()) {  
                System.out.println("Not found file " + fileName);  
                return;  
            }  
            String outputDir = inputFile.getParent();  
            if (isZip) {  
                String outputFileName = inputFile.getName() + ".zip";  
                zip(fileName, outputDir, outputFileName, isPrint);  
            } else {  
                int i = inputFile.getName().indexOf(".");  
                if (i >= 0) {  
                    outputDir += inputFile.getName().substring(0, i);  
                    unZip(fileName, outputDir, isPrint);  
                } else {  
                    System.out.println("File " + fileName + " isn't zip");  
                    return;  
                }  
            }  
        }  
      
        public static void main(String[] args) {  
            // TODO Auto-generated method stub    
            Zip.zipOrUnZip("f:/outdoc", true, true);  
            Zip.zipOrUnZip("f:/outdoc.zip", false, true);  
        }  
    }  

最后可以直接调用zipOrUnZip()方法来实现压缩和解压功能即可

标签: isp 代码

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

上一篇:php获取图片的exif信息

下一篇:生成一定数量的不重复随机数的PHP代码