zoukankan      html  css  js  c++  java
  • Java解压文件代码(相当于你在目录中选中压缩文件 右键解压)

    import java.io.File;
    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.ZipFile;


    public class UnZipper {

        /**
         * 解压文件到当前目录 功能相当于右键 选择解压
         * @param zipFile
         * @param
         * @author gabriel
         */
        @SuppressWarnings("rawtypes")
        public static void unZipFiles(File zipFile)throws IOException{
            //得到压缩文件所在目录
            String path=zipFile.getAbsolutePath();
            path=path.substring(0,path.lastIndexOf("\\"));
           // System.out.println("path "+path);
            ZipFile zip = new ZipFile(zipFile);
            for(Enumeration entries =zip.entries();
                    entries.hasMoreElements();){
                ZipEntry entry = (ZipEntry)entries.nextElement();
                String zipEntryName = entry.getName();
                InputStream in = zip.getInputStream(entry);
                //outPath输出目录
                String outPath = (path+"\\"+zipEntryName).replaceAll("\\*", "/");;
                //System.out.println("outPath "+outPath);
                //判断路径是否存在,不存在则创建文件路径
                File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
                if(!file.exists()){
                    file.mkdirs();
                }
                //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
                if(new File(outPath).isDirectory()){
                    continue;
                }
                //输出文件路径信息
                System.out.println(outPath);
                
                OutputStream out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while((len=in.read(buf1))>0){
                    out.write(buf1,0,len);
                }
                in.close();
                out.close();
                }
            System.out.println("******************解压完毕********************");
        }

       
        public static void main(String[] args) {
            try {
                unZipFiles(new File("D:\\all\\zip\\Default.adiumemoticonset.zip"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

  • 相关阅读:
    洛谷 P2766 最长不下降子序列问【dp+最大流】
    洛谷 P3254 圆桌问题【最大流】
    洛谷 P2764 最小路径覆盖问题【匈牙利算法】
    洛谷 P2763 试题库问题【最大流】
    洛谷 P2762 太空飞行计划问题 【最大权闭合子图+最小割】
    洛谷 P2761 软件补丁问题 【spfa】
    洛谷 P2754 星际转移问题【最大流】
    洛谷 P1251 餐巾计划问题【最小费用最大流】
    spoj 371 Boxes【最小费用最大流】
    poj 3680 Intervals【最小费用最大流】
  • 原文地址:https://www.cnblogs.com/IamThat/p/3037002.html
Copyright © 2011-2022 走看看