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 ~!
------ 如果你说的压缩文件是zip文件的话,那可以用java.util.zip包里的相关类。 用ZipFile来打开zip文件,然后通过getEntry()方法得到子文件的ZipEntry,ZipEntry上的getTime()返回的就是文件的修改时间,类型是long。
如果不是zip而是其它类型的压缩文件,那标准库里就没有直接处理的方法了。 提问者对于答案的评价: 我用ZipFile试了一下,发现可以获取jar里面子文件的时间
感谢RednaxelaFX ~!
------------------------------------------------------------------------------------------------------- 解决中文压缩与解压问题 使用的是org.apache.tools.zip包下面的相关类 下面是自己写的类,可以设置和获取Zip文件的注释信息 - import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.List;
-
- import org.apache.tools.zip.ZipEntry;
- import org.apache.tools.zip.ZipFile;
- import org.apache.tools.zip.ZipOutputStream;
-
-
- public class ZipUtil {
-
- private String comment = "";
-
- public void setComment(String comment) {
- this.comment = comment;
- }
-
-
-
-
-
-
- public void zip(String src, String dest, List filter) throws Exception {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream(dest));
- File srcFile = new File(src);
- zip(out,srcFile,"",filter);
- out.close();
- }
-
-
-
-
-
-
-
-
- public void zip(ZipOutputStream out, File srcFile, String base, List filter) throws Exception {
- if(srcFile.exists()==false) {
- throw new Exception("压缩目录不存在!");
- }
-
- if(srcFile.isDirectory()) {
- File[] files = srcFile.listFiles();
- base = base.length() == 0 ? "" : base + "/";
- if (isExist(base, filter)) {
- out.putNextEntry(new ZipEntry(base));
- }
- for(int i=0; i<files.length; i++) {
- zip(out,files[i],base + files[i].getName(),filter);
- }
- } else {
- if (isExist(base, filter)) {
- base = base.length() == 0 ? srcFile.getName() : base ;
- ZipEntry zipEntry = new ZipEntry(base);
- zipEntry.setComment(comment);
- out.putNextEntry(zipEntry);
- FileInputStream in = new FileInputStream(srcFile);
- int length = 0;
- byte[] b = new byte[1024];
- while((length=in.read(b,0,1024))!=-1) {
- out.write(b,0,length);
- }
- in.close();
- }
- }
- }
-
-
-
-
-
-
-
- public boolean isExist(String base, List list) {
- if (list != null && !list.isEmpty()) {
- for (int i = 0; i < list.size(); i++) {
- if (base.indexOf((String) list.get(i)) >= 0) {
- return true;
- }
- }
- }
- return false;
- }
-
-
-
-
-
-
-
- public void unZip(String srcFile,String dest,boolean deleteFile) throws Exception {
- File file = new File(srcFile);
- if(!file.exists()) {
- throw new Exception("解压文件不存在!");
- }
- ZipFile zipFile = new ZipFile(file);
- Enumeration e = zipFile.getEntries();
- while(e.hasMoreElements()) {
- ZipEntry zipEntry = (ZipEntry)e.nextElement();
- if(zipEntry.isDirectory()) {
- String name = zipEntry.getName();
- name = name.substring(0,name.length()-1);
- File f = new File(dest + name);
- f.mkdirs();
- } else {
- File f = new File(dest + zipEntry.getName());
- f.getParentFile().mkdirs();
- f.createNewFile();
- InputStream is = zipFile.getInputStream(zipEntry);
- FileOutputStream fos = new FileOutputStream(f);
- int length = 0;
- byte[] b = new byte[1024];
- while((length=is.read(b, 0, 1024))!=-1) {
- fos.write(b, 0, length);
- }
- is.close();
- fos.close();
- }
- }
-
- if (zipFile != null) {
- zipFile.close();
- }
-
- if(deleteFile) {
- file.deleteOnExit();
- }
- }
-
-
-
-
-
-
- public static String getZipComment(String srcFile) {
- String comment = "";
- try {
- ZipFile zipFile = new ZipFile(srcFile);
- Enumeration e = zipFile.getEntries();
-
- while (e.hasMoreElements()) {
- ZipEntry ze = (ZipEntry) e.nextElement();
-
- comment = ze.getComment();
- if (comment != null && !comment.equals("")
- && !comment.equals("null")) {
- break;
- }
- }
-
- zipFile.close();
- } catch (Exception e) {
- System.out.println("获取zip文件注释信息失败:" + e.getMessage());
- }
-
- return comment;
- }
-
- public static void main(String[] args) throws Exception {
- long begin = System.currentTimeMillis();
- ZipUtil zu = new ZipUtil();
- List<String> filter = new ArrayList<String>();
- filter.add("3RDPARTY");
- filter.add("BANNER.GIF");
- zu.setComment("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
- zu.zip("C:/VALUEADD", "c:/hh.zip",filter);
- System.out.println(ZipUtil.getZipComment("c:/hh.zip"));
-
-
-
- long end = System.currentTimeMillis();
- System.out.println(end-begin);
- }
- }
|