• java操作zip文件


    思路:

      1)、读取zip中的文件并将除了重名文件之外的文件转存到中转zip文件中。

      2)、往中转文件中插入txt文件。

      3)、删除原zip文件。

      4)、将中转zip文件重命名为原zip文件。

    前提,txt和zip文件需要存在,本代码中未加判断。

    具体代码见下:

    package zip;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;
    
    public class UpdateZip {
    
      private static final byte[] BUFFER = new byte[4096 * 1024];
    
      public static void main(String[] args) throws Exception {
        //路径
        String path = "f:";
        //待插入zip
        String zipname = "a.zip";
        //待插入文件
        String txtname = "c.txt";
        //构建zip和文件准确路径
        String oldzip = path+"/"+zipname;
        //构建中转zip路径
        String newzip = path+"/b.zip";
        String txt = path+"/"+txtname;
        //获取文件
        ZipFile war = new ZipFile(path+"/"+zipname);
        ZipOutputStream append = new ZipOutputStream(new FileOutputStream(newzip));
        //将待插入zip中文件复制到中转zip中
        copytonew(war,append,txtname);
        //往中转zip中插入待插入文件
        insertnewfile(append,txt);
        close(war,append);
        //删除原来zip
        delete(oldzip);
        //将中转zip重命名为原来zip
        rename(oldzip,newzip);
      }
      public static void close(ZipFile war,ZipOutputStream append){
        try {
          war.close();
          append.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      public static void delete(String oldzip){
        File file = new File(oldzip);
        file.delete();
        System.out.println("delete:"+oldzip);
      }
      public static void rename(String oldzip,String newzip){
        File newfile = new File(oldzip);
        File file = new File(newzip);
        file.renameTo(newfile);
        System.out.println("rename:"+oldzip+"------》"+newzip);
      }
      public static void insertnewfile(ZipOutputStream append,String txt){
        File file = new File(txt);
        FileInputStream fis = null; 
        BufferedInputStream bis = null; 
        try { 
          fis = new FileInputStream(file); 
          bis = new BufferedInputStream(fis);
          ZipEntry entry = new ZipEntry(file.getName());
          append.putNextEntry(entry);
          int length; 
          byte[] buffer = new byte[4096]; 
          while ((length = bis.read(buffer)) != -1) { 
            append.write(buffer, 0, length);
            System.out.println("insert:"+txt);
          } 
        } catch (IOException e) { 
          e.printStackTrace(); 
        } finally { 
          try { 
            bis.close(); 
            fis.close(); 
          } catch (IOException e) { 
            e.printStackTrace(); 
          } 
        } 
      }
      public static void copy(InputStream input, OutputStream output) throws IOException {
        int bytesRead;
        while ((bytesRead = input.read(BUFFER))!= -1) {
          output.write(BUFFER, 0, bytesRead);
        }
      }
      public static void copytonew(ZipFile war,ZipOutputStream append,String txtname){
        Enumeration<? extends ZipEntry> entries = war.entries();
        while (entries.hasMoreElements()) {
          ZipEntry e = entries.nextElement();
          if(!e.getName().equals(txtname)){
            System.out.println("copy: " + e.getName());
            try {
              append.putNextEntry(e);
              if (!e.isDirectory()) {
                copy(war.getInputStream(e), append);
              }
              append.closeEntry();
            } catch (IOException e1) {
              e1.printStackTrace();
            } 
          }
        }
      }
    }

    当然,如果你们有好的方法欢迎指导。

  • 相关阅读:
    Conda 中安装 Keras
    Clean Docker :
    Conservation Vs Non-conservation Forms of conservation Equations
    C语言之预处理命令
    const指针用法总结
    C语言中的小启发(陆续更新。。。)
    左值与右值
    计算的次序
    C语言中的符号重载
    C语言之switch语句详解
  • 原文地址:https://www.cnblogs.com/xiufengd/p/4708517.html
Copyright © 2020-2023  润新知