zoukankan      html  css  js  c++  java
  • 大文件下载压缩解析方法

    /** 
     * 功能:压缩多个文件成一个zip文件 
     * @param srcfile:源文件列表 
     * @param zipfile:压缩后的文件 
     */  
    public static void zipFiles(File[] srcfile, File zipfile) {  
        byte[] b = new byte[1024];  
        try {  
            //ZipOutputStream类:完成文件或文件夹的压缩  
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));  
            for (int i = 0; i < srcfile.length; i++) {  
                FileInputStream in = new FileInputStream(srcfile[i]);  
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));  
                int len;  
                while ((len = in.read(b)) > 0) {  
                    out.write(b, 0, len);  
                }  
                out.closeEntry();  
                in.close();  
            }  
            out.close();  
            System.out.println("压缩完成.");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    
     /** 
     * 功能:解压缩 
     * @param zipfile:需要解压缩的文件 
     * @param descDir:解压后的目标目录 
     */  
    @SuppressWarnings({ "rawtypes", "resource" })
    public static void unZipFiles(File zipfile, String descDir) {  
        try {  
            ZipFile zf = new ZipFile(zipfile);  
            for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {  
                ZipEntry entry = (ZipEntry) entries.nextElement();  
                String zipEntryName = entry.getName();  
                InputStream in = zf.getInputStream(entry);  
                OutputStream out = new FileOutputStream(descDir + zipEntryName);  
                byte[] b = new byte[1024];  
                int len;  
                while ((len = in.read(b)) > 0) {  
                    out.write(b, 0, len);  
                }  
                in.close();  
                out.close();  
                System.out.println("解压缩完成.");  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    

     

  • 相关阅读:
    两指针--减少数组循环
    python与正则表达式
    python 获取网页图片
    python学习 第一天
    jquery中的基本理解以及样式属性操作
    webapi中的三大家族
    BOM中的其他对象以及短路运算
    BOM中的api
    事件冒泡和事件捕获
    webapi中注册事件以及解绑事件
  • 原文地址:https://www.cnblogs.com/shenjichenai/p/6232589.html
Copyright © 2011-2022 走看看