CSDN  |   51CTO  |   开源中国  |   Github  |   SM图床网  |   百度一下

JAVA处理文件压缩

  1 package com.ao.zip;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 import java.util.zip.CRC32;
 11 import java.util.zip.CheckedOutputStream;
 12 import java.util.zip.ZipEntry;
 13 import java.util.zip.ZipOutputStream;
 14 
 15 public class FileToZip {
 16     
 17     private static final int BUFFER = 8192;
 18     
 19     private File zipFile;
 20     
 21     public FileToZip(){};
 22     
 23     public FileToZip(String pathName){
 24         zipFile = new File(pathName);
 25         String mkdirPath = pathName.substring(0, pathName.lastIndexOf("/"));
 26         File mkdir = new File(mkdirPath);
 27         if(!mkdir.exists()){
 28             mkdir.mkdirs();
 29         }
 30     };
 31     
 32     public FileToZip(String pathName, String fileName){
 33         zipFile = new File(pathName + "/" + fileName);
 34         File mkdir = new File(pathName);
 35         if(!mkdir.exists()){
 36             mkdir.mkdirs();
 37         }
 38     };
 39     
 40     /**
 41      * 将存放在sourceFilePath目录下的源文件,打包成fileName名称的zip文件,并存放到zipFilePath路径下
 42      * @param sourceFilePath :待压缩的文件路径
 43      * @param zipFilePath :压缩后存放路径
 44      * @param fileName :压缩后文件的名称
 45      * @return
 46      */
 47     public static boolean fileToZip(String sourceFilePath, 
 48             String zipFilePath, String fileName){
 49         boolean flag = false;
 50         File sourceFile = new File(sourceFilePath);
 51         FileInputStream fis = null;
 52         BufferedInputStream bis = null;
 53         FileOutputStream fos = null;
 54         ZipOutputStream zos = null;
 55         if(!sourceFile.exists()){
 56             System.err.println("待压缩的文件目录:" + sourceFilePath + "不存在.");
 57         }else{
 58             try {
 59                 File zippath = new File(zipFilePath);
 60                 if(!zippath.exists()){
 61                     zippath.mkdirs();
 62                 }
 63                 File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
 64                 if(zipFile.exists()){
 65                     System.err.println(zipFilePath + "目录下存在文件名为:" + fileName + ".zip的打包文件.");
 66                     System.err.println("正在删除中...");
 67                     System.err.println(zipFilePath + "/" + fileName + ".zip");
 68                     zipFile.delete();
 69                 }
 70                 File[] sourceFiles = sourceFile.listFiles();
 71                 if(sourceFiles == null || sourceFiles.length < 1){
 72                     System.err.println("待压缩的文件目录:" + sourceFilePath  + "里面不存在文件,无需压缩.");
 73                 }else{
 74                     fos = new FileOutputStream(zipFile);
 75                     zos = new ZipOutputStream(new BufferedOutputStream(fos));
 76                     byte[] bufs = new byte[1024*10];
 77                     for(int i = 0; i < sourceFiles.length; i++){
 78                         //创建ZIP实体,并添加进压缩包
 79                         ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
 80                         zos.putNextEntry(zipEntry);
 81                         //读取待压缩的文件并写进压缩包里
 82                         fis = new FileInputStream(sourceFiles[i]);
 83                         bis = new BufferedInputStream(fis, 1024*10);
 84                         int read = 0;
 85                         while((read = bis.read(bufs, 0, 1024*10)) != -1){
 86                             zos.write(bufs, 0, read);
 87                         }
 88                     }
 89                     flag = true;
 90                 }
 91             }catch (FileNotFoundException  e) {
 92                 e.printStackTrace();
 93                 throw new RuntimeException(e);
 94             }catch (IOException  e) {
 95                 e.printStackTrace();
 96                 throw new RuntimeException(e);
 97             }finally{//关闭流
 98                 try{
 99                     if(bis != null){
100                         bis.close();
101                     }
102                     if(zos != null){
103                         zos.close();
104                     }
105                 }catch(IOException e){
106                     e.printStackTrace();
107                     throw new RuntimeException(e);
108                 }
109             }
110         }
111         return flag;
112     }
113     
114     public void fileToZip(String...pathName){
115         ZipOutputStream out = null;
116         try{
117             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
118             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
119             out = new ZipOutputStream(cos);
120             String basedir = "";
121             for(int i = 0; i < pathName.length; i++){
122                 fileToZip(new File(pathName[i]), out, basedir);
123             }
124         }catch(FileNotFoundException e){
125             e.printStackTrace();
126             throw new RuntimeException(e);
127         }finally{
128             try{
129                 if(out != null){
130                     out.close();
131                 }
132             }catch(IOException e){
133                 e.printStackTrace();
134                 throw new RuntimeException(e);
135             }
136         }
137     }
138     
139     public void fileToZip(String srcPathName){
140         File file = new File(srcPathName);    
141         ZipOutputStream out = null;
142         if(!file.exists()){
143             throw new RuntimeException(srcPathName + "不存在!");     
144         }   
145         try{     
146             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);     
147             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
148                     new CRC32());     
149             out = new ZipOutputStream(cos);     
150             String basedir = "";     
151             fileToZip(file, out, basedir);     
152         }catch(FileNotFoundException e) {     
153             throw new RuntimeException(e);     
154         }finally{
155             try{
156                 if(out != null){
157                     out.close();
158                 }
159             }catch(IOException e){
160                 e.printStackTrace();
161                 throw new RuntimeException(e);
162             }
163         }     
164     }
165     
166     private void fileToZip(File file, ZipOutputStream out, String basedir) {     
167         /* 判断是目录还是文件 */    
168         if (file.isDirectory()) {     
169             System.out.println("压缩:" + basedir + file.getName());     
170             this.fileToZipDirectory(file, out, basedir);     
171         } else {     
172             System.out.println("压缩:" + basedir + file.getName());     
173             this.fileToZipFile(file, out, basedir);     
174         }     
175     }
176     
177     /** 压缩一个目录 */    
178     private void fileToZipDirectory(File dir, ZipOutputStream out, String basedir) {     
179         if (!dir.exists())     
180             return;
181     
182         File[] files = dir.listFiles();     
183         for (int i = 0; i < files.length; i++) {     
184             /* 递归 */    
185             fileToZip(files[i], out, basedir + dir.getName() + "/");     
186         }     
187     }     
188     
189     /** 压缩一个文件 */    
190     private void fileToZipFile(File file, ZipOutputStream out, String basedir) {     
191         if(!file.exists()){
192             return;
193         }
194         try{
195             BufferedInputStream bis = new BufferedInputStream(     
196                     new FileInputStream(file));     
197             ZipEntry entry = new ZipEntry(basedir + file.getName());     
198             out.putNextEntry(entry);     
199             int count;
200             byte data[] = new byte[BUFFER];     
201             while ((count = bis.read(data, 0, BUFFER)) != -1) {     
202                 out.write(data, 0, count);     
203             }
204             bis.close();     
205         } catch (Exception e) {
206             throw new RuntimeException(e);
207         }
208     }
209     
210 //    public static void main(String[] args) {
211 //        String sourceFilePath = "E:/Test/aaa.txt";
212 //        String zipFilePath = "E:";
213 //        String fileName = "aaa";
214 //        System.err.println(fileToZip(sourceFilePath, zipFilePath, fileName));
215 //        FileToZip zc = new FileToZip("E:/resource", "resource.zip");
216 //        zc.fileToZip("E:/Test", "E:/临洮");
217 //    }
218 
219 }

 

posted @ 2018-07-31 17:58  芲落  阅读(534)  评论(0编辑  收藏  举报