一个撇脚的java压缩文件工具类

今天弄里一个压缩文件的工具类,功能不是很完善,只支持压缩后单级目录,二级文件夹的文件名好像只能用中文,并且解压后还是乱码。请各位高手大侠批评指教,不胜感激!

  
  
  
  
  1. package tests;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.util.zip.ZipEntry;  
  10. import java.util.zip.ZipOutputStream;  
  11.  
  12. public class ZipUtil {  
  13.     private static File zip;  
  14.     private static ZipOutputStream zipOut;  
  15.     private InputStream input;  
  16.     public ZipUtil(){  
  17.         try {  
  18.             zipOut = new ZipOutputStream(new FileOutputStream(zip));  
  19.             zipOut.setComment("hello2");  
  20.         } catch (FileNotFoundException e) {  
  21.             e.printStackTrace();  
  22.         }  
  23.     }  
  24.     public void zipFile(File file) {  
  25.         if (file.isDirectory()) {  
  26.             File[] files = file.listFiles();  
  27.             for (int i = 0; i < files.length; i++) {  
  28.                 if (files[i].isDirectory()) {  
  29.                     zipFile(files[i]);  
  30.                 } else {  
  31.                     try {  
  32.                         input = new FileInputStream(files[i]);  
  33.                         zipOut.putNextEntry(new ZipEntry(file.getName()  
  34.                                 + File.separator + files[i].getName()));  
  35.                         int temp = 0;  
  36.                         while ((temp = input.read()) != -1) {  
  37.                             zipOut.write(temp);  
  38.                         }  
  39.                         input.close();  
  40.                     } catch (FileNotFoundException e) {  
  41.                         e.printStackTrace();  
  42.                     } catch (IOException e) {  
  43.                         e.printStackTrace();  
  44.                     }  
  45.                 }  
  46.             }  
  47.             try {  
  48.                 zipOut.close();  
  49.             } catch (IOException e) {  
  50.                 e.printStackTrace();  
  51.             }  
  52.         } else {  
  53.             try {  
  54.                 input = new FileInputStream(file);  
  55.                 zipOut.putNextEntry(new ZipEntry(file.getName()));  
  56.                 int temp = 0;  
  57.                 while ((temp = input.read()) != -1) {  
  58.                     zipOut.write(temp);  
  59.                 }  
  60.                 input.close();  
  61.                 zipOut.close();  
  62.             } catch (FileNotFoundException e) {  
  63.                 e.printStackTrace();  
  64.             } catch (IOException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.     }  
  69.     public static void main(String[] args) {  
  70.         zip = new File("d:" + File.separator + "hello.rar");  
  71.         File file = new File("d:" + File.separator + "hello");  
  72.         new ZipUtil().zipFile(file);  
  73.     }  

 

你可能感兴趣的:(中文,文件夹,压缩文件,不胜感激,tests)