Blame view

src/com/fh/util/FileZip.java 2.15 KB
ad5081d3   孙向锦   初始化项目
1
2
3
4
5
6
7
  package com.fh.util;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.util.zip.ZipEntry;
  import java.util.zip.ZipOutputStream;
  
ad5081d3   孙向锦   初始化项目
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  public class FileZip {
  
  	/**
  	 * @param inputFileName 你要压缩的文件夹(整个完整路径)
  	 * @param zipFileName 压缩后的文件(整个完整路径)
  	 * @throws Exception
  	 */
  	public static Boolean zip(String inputFileName, String zipFileName) throws Exception {
  		zip(zipFileName, new File(inputFileName));
  		return true;
  	}
  
  	private static void zip(String zipFileName, File inputFile) throws Exception {
  		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
  		zip(out, inputFile, "");
  		out.flush();
  		out.close();
  	}
  
  	private static void zip(ZipOutputStream out, File f, String base) throws Exception {
  		if (f.isDirectory()) {
  			File[] fl = f.listFiles();
  			out.putNextEntry(new ZipEntry(base + "/"));
  			base = base.length() == 0 ? "" : base + "/";
  			for (int i = 0; i < fl.length; i++) {
  				zip(out, fl[i], base + fl[i].getName());
  			}
  		} else {
  			out.putNextEntry(new ZipEntry(base));
  			FileInputStream in = new FileInputStream(f);
  			int b;
  			//System.out.println(base);
  			while ((b = in.read()) != -1) {
  				out.write(b);
  			}
  			in.close();
  		}
  	}
  	
  	 public static void main(String [] temp){       
  		 try {           
  			 zip("E:\\ftl","E:\\test.zip");//你要压缩的文件夹      和  压缩后的文件 
  			 }catch (Exception ex) {       
  				 ex.printStackTrace();    
  			 }   
  		}
  }
  
  
  
  //=====================文件压缩=========================
  /*//把文件压缩成zip
  File zipFile = new File("E:/demo.zip");
  //定义输入文件流
  InputStream input = new FileInputStream(file);
  //定义压缩输出流	
  ZipOutputStream zipOut = null;
  //实例化压缩输出流,并制定压缩文件的输出路径  就是E盘下,名字叫 demo.zip
  zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
  zipOut.putNextEntry(new ZipEntry(file.getName()));
  //设置注释
  zipOut.setComment("www.demo.com");
  int temp = 0;
  while((temp = input.read()) != -1) {
  	zipOut.write(temp);	
  }		
  input.close();
  zipOut.close();*/
  //==============================================