zoukankan      html  css  js  c++  java
  • java文件压缩并下载

    1. import java.io.File;  
    2. import java.io.FileInputStream;  
    3. import java.io.FileNotFoundException;  
    4. import java.io.FileOutputStream;  
    5. import java.io.IOException;  
    6. import java.util.zip.ZipEntry;  
    7. import java.util.zip.ZipOutputStream;  
    8.   
    9. public class TestZip {  
    10.   
    11.     private void zip(String souceFileName, String destFileName) {  
    12.         File file = new File(souceFileName);  
    13.         try {  
    14.             zip(file, destFileName);  
    15.         } catch (IOException e) {  
    16.             e.printStackTrace();  
    17.         }  
    18.     }  
    19.   
    20.     private void zip(File souceFile, String destFileName) throws IOException {  
    21.         FileOutputStream fileOut = null;  
    22.         try {  
    23.             fileOut = new FileOutputStream(destFileName);  
    24.         } catch (FileNotFoundException e) {  
    25.             e.printStackTrace();  
    26.         }  
    27.         ZipOutputStream out = new ZipOutputStream(fileOut);  
    28.         zip(souceFile, out, "");  
    29.         out.close();  
    30.     }  
    31.   
    32.     private void zip(File souceFile, ZipOutputStream out, String base)  
    33.             throws IOException {  
    34.   
    35.         if (souceFile.isDirectory()) {  
    36.             File[] files = souceFile.listFiles();  
    37.             out.putNextEntry(new ZipEntry(base + "/"));  
    38.             base = base.length() == 0 ? "" : base + "/";  
    39.             for (File file : files) {  
    40.                 zip(file, out, base + file.getName());  
    41.             }  
    42.         } else {  
    43.             if (base.length() > 0) {  
    44.                 out.putNextEntry(new ZipEntry(base));  
    45.             } else {  
    46.                 out.putNextEntry(new ZipEntry(souceFile.getName()));  
    47.             }  
    48.             System.out.println("filepath=" + souceFile.getPath());  
    49.             FileInputStream in = new FileInputStream(souceFile);  
    50.   
    51.             int b;  
    52.             byte[] by = new byte[1024];  
    53.             while ((b = in.read(by)) != -1) {  
    54.                 out.write(by, 0, b);  
    55.             }  
    56.             in.close();  
    57.         }  
    58.     }  
    59.   
    60.     /** 
    61.      * Test 
    62.      */  
    63.     public static void main(String[] args) {  
    64.         TestZip z = new TestZip();  
    65.         z.zip("D:\image11", "D:/Data1.zip");  
    66.     }  
    67. }  

    /其中 
    z.zip("D:\image11", "D:/Data1.zip"); 
    第一个参数是标识要生成目录的文件夹这个会遍历下面所有的文件夹中的文件 
    第二个参数 生成的zip的文件位置 

     

  • 相关阅读:
    关于重构的一些方法
    java基础 逻辑
    java基础
    去重和数组排序
    表单验证
    JS实例5
    window.document对象
    JS实例4
    JS实例3
    JS实例2
  • 原文地址:https://www.cnblogs.com/fg-fd/p/6898274.html
Copyright © 2011-2022 走看看