zip.java

  1. /*
  2.  *這個類用來壓縮和解壓
  3.  *
  4.  * */
  5. package server;
  6. import java.io.BufferedInputStream;
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.util.Enumeration;
  12. import java.util.zip.ZipEntry;
  13. import java.util.zip.ZipFile;
  14. import java.util.zip.ZipOutputStream;
  15. public class Zip {
  16.      public  static int BUFFER;
  17.     public Zip()
  18.     {
  19.     BUFFER = 2048;
  20.     }
  21.     public void Yasuo(String test,String ff)
  22.     {
  23.         try {
  24.             BufferedInputStream origin = null;
  25.           //  FileOutputStream dest = new FileOutputStream("E://test//myfiles.zip");
  26.             FileOutputStream dest = new FileOutputStream(test);
  27.             ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream (dest));
  28.             byte data[] = new byte[BUFFER];
  29.            // File f = new File("e://test//a//");
  30.             File f = new File(ff);
  31.             File files[] = f.listFiles();
  32.             for (int i = 0; i < files.length; i++) {
  33.                 FileInputStream fi = new FileInputStream(files[i]);
  34.                 origin = new BufferedInputStream(fi, BUFFER);
  35.                 ZipEntry entry = new ZipEntry(files[i].getName());
  36.                 out.putNextEntry(entry);
  37.                 int count;
  38.                 while ((count = origin.read(data, 0, BUFFER)) != -1) {
  39.                     out.write(data, 0, count);
  40.                 }
  41.                 origin.close();
  42.             }
  43.             out.close();
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.     
  49.      public void Jieya(String name,String path)
  50.       {
  51.             try {
  52.               //  String fileName = "E://test//myfiles.zip";
  53.              //   String filePath = "E://test//";
  54.                 String fileName=name;
  55.                 String filePath=path;
  56.                 ZipFile zipFile = new ZipFile(fileName);
  57.                 Enumeration emu = zipFile.entries();
  58.                 int i=0;        
  59.                 while(emu.hasMoreElements()){
  60.                     ZipEntry entry = (ZipEntry)emu.nextElement();
  61.                     //會把目錄作爲一個file讀出一次,所以只建立目錄就可以,之下的文件還會被迭代到。
  62.                     if (entry.isDirectory())
  63.                     {
  64.                         new File(filePath + entry.getName()).mkdirs();
  65.                         continue;
  66.                     }
  67.                     BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
  68.                     File file = new File(filePath + entry.getName());
  69.                     //加入這個的原因是zipfile讀取文件是隨機讀取的,這就造成可能先讀取一個文件
  70.                     //而這個文件所在的目錄還沒有出現過,所以要建出目錄來。
  71.                     File parent = file.getParentFile();
  72.                     if(parent != null && (!parent.exists())){
  73.                         parent.mkdirs();
  74.                     }
  75.                     FileOutputStream fos = new FileOutputStream(file);
  76.                     BufferedOutputStream bos  = new BufferedOutputStream(fos,BUFFER);           
  77.                     
  78.                     int count;
  79.                     byte data[] = new byte[BUFFER];
  80.                     while ((count = bis.read(data, 0, BUFFER)) != -1)
  81.                     {
  82.                         bos.write(data, 0, count);
  83.                     }
  84.                     bos.flush();
  85.                     bos.close();
  86.                     bis.close();
  87.                 }
  88.                 zipFile.close();
  89.             } catch (Exception e) {
  90.                 e.printStackTrace();
  91.             }
  92.             }
  93.     } 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章