zoukankan      html  css  js  c++  java
  • JAVA --解压缩

    package com.DecompressFile;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;
    
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipFile;
    /**
     *可以解压包含多种文件多层目录的压缩文件 
     */
    public class Test3 {
        private Test3(){}
        public synchronized void unZip(String zipPath,String outPath) throws IOException{
            File zipFile = new File(zipPath);
            File outFile = new File(outPath);
            if(!zipFile.exists() || !zipFile.isFile()){
                System.err.println("压缩文件错误!");
            }else{
                boolean f = isName(zipFile);
                if(!f){
                    System.err.println("要解压的文件格式错误");
                    System.exit(0);
                }
                
                if(! outFile.exists() && !outFile.isDirectory()){
                    outFile.mkdir();
                }
                ZipFile zip = new ZipFile(zipPath);
                Enumeration en = zip.getEntries();
                ZipEntry entity = null ;
                while(en.hasMoreElements()){
                    entity = (ZipEntry)en.nextElement();
                    if(entity.isDirectory()){
                        String folder = entity.getName();
                        folder = folder.substring(0, folder.length() - 1);
                        File file = new File(outFile.getPath() + File.separator + folder);
                        file.mkdir();
                    }else{
                        String sname = outFile.getPath() + File.separator + entity.getName();
                        File file = new File(sname);
                        if(!file.exists()){
                            String names[] = entity.getName().split("/");
                            String ss = "";
                            for(int i = 0;i < names.length - 1;i++){
                                ss += names[i] + File.separator;
                            }
                            ss = outFile.getPath() + File.separator + ss;
                            File ssfile = new File(ss);
                            ssfile.mkdir();
                        }
                        file.createNewFile();
                        InputStream in = zip.getInputStream(entity);
                        FileOutputStream fos = new FileOutputStream(file);
                        try{
                            int c;
                            byte bytes[] = new byte[1024];
                            while((c = in.read(bytes)) != -1){
                                fos.write(bytes, 0, c);
                            }
                        }catch (Exception e) {
                            e.printStackTrace();
                        }finally{
                            fos.close();
                            in.close();
                        }
                    }
                }
            }
        }
        
        public static boolean isName(File file){
            String path = file.getPath();
            String name = path.substring(path.lastIndexOf("."),path.length());
            if(name.equals(".zip") || name.equals(".tgz") || name.equals(".tar")){
                return true;
            }
            return false;
        }
        
        public static void main(String args[]){
            String zipPath = "E:/test.zip";
            String outPath = "E:/test12345";
            Test3 t3 = new Test3();
            try {
                t3.unZip(zipPath, outPath);
                System.out.println("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Vue父子组件传值之——访问根组件$root、$parent、$children和$refs
    js判断是否为ie浏览器,精确显示各个ie版本
    在JS/jQuery中,怎么触发input的keypress/keydown/keyup事件?
    HTML中a标签自动识别电话、邮箱
    如何彻底删除Mac磁盘中的文件
    使用Understand for Mac编写您的第一个API脚本
    如何将MacOS Catalina降级为Mojave
    macOS Catalina 10.15.1 发布 全新 Emoji、支持 AirPods Pro
    WingIDE Pro 7如何新建项目
    忘记MacBook密码的解决技巧!
  • 原文地址:https://www.cnblogs.com/zqzdong/p/4840356.html
Copyright © 2011-2022 走看看