错误java.util.zip.ZipException:将图像(.png)从1个zip文件复制到另一个文件时,条目大小无效 [英] Error java.util.zip.ZipException: invalid entry size while copying image (.png) from 1 zip file to another

查看:129
本文介绍了错误java.util.zip.ZipException:将图像(.png)从1个zip文件复制到另一个文件时,条目大小无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改现有的.zip文件,然后创建修改后的副本.

I'm trying to modify an existing .zip file and then creating a modified copy.

我可以轻松地对所有文件执行此操作,但zip文件中的.png文件除外,这会导致错误

I can easily do that to all files except for a .png file in the zip file, which results in error

java.util.zip.ZipException:无效的条目压缩大小(预期为113177,但获得了113312字节)

java.util.zip.ZipException: invalid entry compressed size (expected 113177 but got 113312 bytes)

下面的代码是我试图从dice.zip复制一个.png图像并将其添加到diceUp.zip中的操作.

The following code is what I'm trying to run to simply copy a .png image from dice.zip and adding it to diceUp.zip.

public class Test {
public static void main(String[] args) throws IOException{
    ZipFile zipFile = new ZipFile("dice.zip");
    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("diceUp.zip"));
    for(Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
        ZipEntry entryIn = (ZipEntry) e.nextElement();
            if(entryIn.getName().contains(".png")){
                System.out.println(entryIn.getName());
                zos.putNextEntry(entryIn);
                InputStream is = zipFile.getInputStream(entryIn);
                byte [] buf = new byte[1024];
                int len;
                while((len = (is.read(buf))) > 0) {            
                    zos.write(buf, 0, (len < buf.length) ? len : buf.length);
                }
        }
        zos.closeEntry();
    }
    zos.close();
}

推荐答案

只需创建名称为 entryIn 对象的新 ZipEntry 对象,并将此新对象放入 zos.putNextEntry .
查看此问题!
这是我的代码:

Just create new ZipEntry object with the name of entryIn object and put this new object in zos.putNextEntry .
Look at this qustion!
And this is my code:

    public static void main(String[] args) throws IOException {
    ZipFile zipFile = new ZipFile("/home/*********/resources/dice.zip");
//        ZipFile zipFile = new ZipFile();
    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("diceUp.zip"));
    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
        ZipEntry entryIn = e.nextElement();
        if (entryIn.getName().contains(".png")) {
            System.out.println(entryIn.getName());
            ZipEntry zipEntry = new ZipEntry(entryIn.getName());
            zos.putNextEntry(zipEntry);
            InputStream is = zipFile.getInputStream(entryIn);
            byte[] buf = new byte[1024];
            int len;
            while ((len = (is.read(buf))) > 0) {
//                    zos.write(buf, 0, (len < buf.length) ? len : buf.length);
                zos.write(buf);
            }
        }
        zos.closeEntry();
    }
    zos.close();
}

这篇关于错误java.util.zip.ZipException:将图像(.png)从1个zip文件复制到另一个文件时,条目大小无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆