java向zip文件中添加新文件

import java.io.File;
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 Main {

    // 4MB buffer
    private static final byte[] BUFFER = new byte[4096 * 1024];

    /**
     * copy input to output stream - available in several StreamUtils or Streams classes 
     */    
    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 main(String[] args) throws Exception {
        // read war.zip and write to append.zip
        ZipFile war = new ZipFile("war.zip");
        ZipOutputStream append = new ZipOutputStream(new FileOutputStream("append.zip"));

        // first, copy contents from existing war
        Enumeration<? extends ZipEntry> entries = war.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            System.out.println("copy: " + e.getName());
            append.putNextEntry(e);
            if (!e.isDirectory()) {
                copy(war.getInputStream(e), append);
            }
            append.closeEntry();
        }

        // now append some extra content
        ZipEntry e = new ZipEntry("answer.txt");
        System.out.println("append: " + e.getName());
        append.putNextEntry(e);
        append.write("42\n".getBytes());
        append.closeEntry();

        // close
        war.close();
        append.close();
    }
}

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用JavaZipFile和ZipOutputStream类来修改一个zip文件文件。首先,使用ZipFile类打开zip文件,并获取到需要修改的文件ZipEntry对象。然后,使用ZipOutputStream类创建一个zip文件,在其添加修改后的文件内容。最后,将修改后的zip文件保存到磁盘上,并删除原始的zip文件。 下面是一个示例代码,演示如何使用Java修改zip文件文件: ```java import java.io.*; import java.util.zip.*; public class ZipFileModifier { public static void modifyFileInZip(String zipFilePath, String fileToModifyPath, String newContent) throws IOException { // Open the zip file File zipFile = new File(zipFilePath); ZipFile zip = new ZipFile(zipFile); // Get the entry for the file to modify ZipEntry fileToModify = zip.getEntry(fileToModifyPath); // Create a new zip file File newZipFile = new File(zipFile.getParentFile(), "modified.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(newZipFile)); // Copy all entries from the original zip file to the new zip file, except for the file to modify Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (!entry.equals(fileToModify)) { out.putNextEntry(entry); InputStream in = zip.getInputStream(entry); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.closeEntry(); in.close(); } } // Add the modified file to the new zip file out.putNextEntry(new ZipEntry(fileToModifyPath)); out.write(newContent.getBytes()); out.closeEntry(); // Close the streams out.close(); zip.close(); // Delete the original zip file and rename the new zip file to the original name if (zipFile.delete()) { newZipFile.renameTo(zipFile); } } } ``` 上面的代码,modifyFileInZip方法接受三个参数:zipFilePath表示zip文件的路径,fileToModifyPath表示需要修改的文件的路径,newContent表示修改后的文件内容。该方法会首先打开zip文件,获取到需要修改的文件ZipEntry对象。然后,创建一个zip文件,并从原始zip文件复制所有文件zip文件,除了需要修改的文件。最后,将修改后的文件内容添加zip文件,并将zip文件保存到磁盘上。最后,删除原始的zip文件,并将zip文件重命名为原始zip文件的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值