分享

关于JAVA ZIP追加新文件问题 —— Java实现Zip压缩与解压(解决中文乱码问题)

 CevenCheng 2011-05-12
JAVA中ZipOutputStream是不支持直接向原.zip文件以追加方式添加文件的。如果需要实现,要将原zip文件都出,然后重新写入新zip文件中,最后写入要追加的文件。

----------------------------------------------------------
我遇到这种情况该怎么做?就是现在存在一个zip文件,但是我需要往这个zip里面加一个或几个文件.把他们一起放在这个zip里但又不能影响它之前的文件,这个该怎么做?

解压缩 然后删了压缩文件 把新的文件放进去 然后再压缩吧

1、把zip文件解压到一个临时目录。 
2、按照你要求的路径上传一个或多个文件到对应的临时目录下。 
3、在临时目录下生成新的zip包。 
4、覆盖原zip包。 
5、删除临时目录。
----------------------------------------------------------

请问JAVA怎样获取压缩文件里子文件的最后修改时间?
问题补充:
注明:压缩文件的格式是 *.jar 

发现java.util.jar.ZipEntry 里面没有获取时间的方法呀
问题补充:


我用ZipFile试了一下,发现可以获取jar里面子文件的时间 

感谢RednaxelaFX ~!

------

2008-12-22 RednaxelaFX (架构师)

如果你说的压缩文件是zip文件的话,那可以用java.util.zip包里的相关类。 
用ZipFile来打开zip文件,然后通过getEntry()方法得到子文件的ZipEntry,ZipEntry上的getTime()返回的就是文件的修改时间,类型是long。 

如果不是zip而是其它类型的压缩文件,那标准库里就没有直接处理的方法了。

提问者对于答案的评价:
我用ZipFile试了一下,发现可以获取jar里面子文件的时间 

感谢RednaxelaFX ~!

-------------------------------------------------------------------------------------------------------
解决中文压缩与解压问题 
使用的是org.apache.tools.zip包下面的相关类 
下面是自己写的类,可以设置和获取Zip文件的注释信息 
Java代码  收藏代码
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.InputStream;  
  5. import java.util.ArrayList;  
  6. import java.util.Enumeration;  
  7. import java.util.List;  
  8.   
  9. import org.apache.tools.zip.ZipEntry;  
  10. import org.apache.tools.zip.ZipFile;  
  11. import org.apache.tools.zip.ZipOutputStream;  
  12.   
  13.   
  14. public class ZipUtil {  
  15.       
  16.     private String comment = "";  
  17.       
  18.     public void setComment(String comment) {  
  19.         this.comment = comment;  
  20.     }  
  21.   
  22.     /** 
  23.      * @param src:要压缩的目录 
  24.      * @param dest:压缩文件存档 
  25.      * @throws Exception 
  26.      */  
  27.     public void zip(String src, String dest, List filter) throws Exception {  
  28.         ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));  
  29.         File srcFile = new File(src);  
  30.         zip(out,srcFile,"",filter);  
  31.         out.close();  
  32.     }  
  33.       
  34.     /** 
  35.      * @param out 
  36.      * @param srcFile 
  37.      * @param base:根路径 
  38.      * @param filter:过滤 
  39.      * @throws Exception 
  40.      */  
  41.     public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {  
  42.         if(srcFile.exists()==false) {  
  43.             throw new Exception("压缩目录不存在!");  
  44.         }  
  45.           
  46.         if(srcFile.isDirectory()) {  
  47.             File[] files = srcFile.listFiles();  
  48.             base = base.length() == 0 ? "" : base + "/";  
  49.             if (isExist(base, filter)) {  
  50.                 out.putNextEntry(new ZipEntry(base));  
  51.             }  
  52.             for(int i=0; i<files.length; i++) {  
  53.                 zip(out,files[i],base + files[i].getName(),filter);  
  54.             }  
  55.         } else {  
  56.             if (isExist(base, filter)) {  
  57.                 base = base.length() == 0 ? srcFile.getName() : base ;  
  58.                 ZipEntry zipEntry = new ZipEntry(base);  
  59.                 zipEntry.setComment(comment);  
  60.                 out.putNextEntry(zipEntry);  
  61.                 FileInputStream in = new FileInputStream(srcFile);  
  62.                 int length = 0;  
  63.                 byte[] b = new byte[1024];  
  64.                 while((length=in.read(b,0,1024))!=-1) {  
  65.                     out.write(b,0,length);  
  66.                 }  
  67.                 in.close();  
  68.             }  
  69.         }  
  70.     }  
  71.       
  72.     /** 
  73.      * 过滤出要压缩的文件夹 
  74.      * @param base 
  75.      * @param list 
  76.      * @return 
  77.      */  
  78.     public boolean isExist(String base, List list) {  
  79.         if (list != null && !list.isEmpty()) {  
  80.             for (int i = 0; i < list.size(); i++) {  
  81.                 if (base.indexOf((String) list.get(i)) >= 0) {  
  82.                     return true;  
  83.                 }  
  84.             }  
  85.         }  
  86.         return false;  
  87.     }  
  88.       
  89.     /** 
  90.      * @param srcFile:压缩文件路径 
  91.      * @param dest:解压到的目录 
  92.      * @param deleteFile:解压完成后是否删除文件 
  93.      * @throws Exception 
  94.      */  
  95.     public void unZip(String srcFile,String dest,boolean deleteFile)  throws Exception {  
  96.         File file = new File(srcFile);  
  97.         if(!file.exists()) {  
  98.             throw new Exception("解压文件不存在!");  
  99.         }  
  100.         ZipFile zipFile = new ZipFile(file);  
  101.         Enumeration e = zipFile.getEntries();  
  102.         while(e.hasMoreElements()) {  
  103.             ZipEntry zipEntry = (ZipEntry)e.nextElement();  
  104.             if(zipEntry.isDirectory()) {  
  105.                 String name = zipEntry.getName();  
  106.                 name = name.substring(0,name.length()-1);  
  107.                 File f = new File(dest + name);  
  108.                 f.mkdirs();  
  109.             } else {  
  110.                 File f = new File(dest + zipEntry.getName());  
  111.                 f.getParentFile().mkdirs();  
  112.                 f.createNewFile();  
  113.                 InputStream is = zipFile.getInputStream(zipEntry);  
  114.                 FileOutputStream fos = new FileOutputStream(f);  
  115.                 int length = 0;  
  116.                 byte[] b = new byte[1024];  
  117.                 while((length=is.read(b, 01024))!=-1) {  
  118.                     fos.write(b, 0, length);  
  119.                 }  
  120.                 is.close();  
  121.                 fos.close();  
  122.             }  
  123.         }  
  124.           
  125.         if (zipFile != null) {  
  126.             zipFile.close();  
  127.         }  
  128.           
  129.         if(deleteFile) {  
  130.             file.deleteOnExit();  
  131.         }  
  132.     }  
  133.       
  134.     /** 
  135.      * 获取Zip文件的注释信息 
  136.      * @param srcFile 
  137.      * @return 
  138.      */  
  139.     public static String getZipComment(String srcFile) {  
  140.         String comment = "";  
  141.         try {  
  142.             ZipFile zipFile = new ZipFile(srcFile);  
  143.             Enumeration e = zipFile.getEntries();  
  144.   
  145.             while (e.hasMoreElements()) {  
  146.                 ZipEntry ze = (ZipEntry) e.nextElement();  
  147.   
  148.                 comment = ze.getComment();  
  149.                 if (comment != null && !comment.equals("")  
  150.                         && !comment.equals("null")) {  
  151.                     break;  
  152.                 }  
  153.             }  
  154.   
  155.             zipFile.close();  
  156.         } catch (Exception e) {  
  157.             System.out.println("获取zip文件注释信息失败:" + e.getMessage());  
  158.         }  
  159.   
  160.         return comment;  
  161.     }  
  162.   
  163.     public static void main(String[] args) throws Exception {  
  164.         long begin = System.currentTimeMillis();  
  165.         ZipUtil zu = new ZipUtil();  
  166.         List<String> filter = new ArrayList<String>();  
  167.         filter.add("3RDPARTY");  
  168.         filter.add("BANNER.GIF");  
  169.         zu.setComment("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");  
  170.         zu.zip("C:/VALUEADD""c:/hh.zip",filter);  
  171.         System.out.println(ZipUtil.getZipComment("c:/hh.zip"));  
  172.         //new ZipUtil().unZip("c:/tt.zip", "c:/mmmmmmmmmmmmmmmmmmm/", true);  
  173.         //File f = new File("c:/hh.zip");  
  174.         //f.deleteOnExit();  
  175.         long end = System.currentTimeMillis();  
  176.         System.out.println(end-begin);  
  177.     }  
  178. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多