错误java.util.zip.ZipException:将图像(.png)从一个zip文件复制到另一个zip文件时,条目大小无效
我正在尝试修改现有的.zip文件,然后创建一个修改后的副本 除了zip文件中的.png文件,我可以轻松地对所有文件执行此操作,这会导致错误 java.util.zip.ZipException:条目压缩大小无效(预期为113177,但得到113312字节) 下面的代码是我试图运行的,它只是从dice.zip复制一个.png图像并将其添加到diceUp.zip错误java.util.zip.ZipException:将图像(.png)从一个zip文件复制到另一个zip文件时,条目大小无效,java,zip,png,inputstream,zipoutputstream,Java,Zip,Png,Inputstream,Zipoutputstream,我正在尝试修改现有的.zip文件,然后创建一个修改后的副本 除了zip文件中的.png文件,我可以轻松地对所有文件执行此操作,这会导致错误 java.util.zip.ZipException:条目压缩大小无效(预期为113177,但得到113312字节) 下面的代码是我试图运行的,它只是从dice.zip复制一个.png图像并将其添加到diceUp.zip public class Test { public static void main(String[] args) throws IOE
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();
}
公共类测试{
公共静态void main(字符串[]args)引发IOException{
ZipFile ZipFile=新ZipFile(“dice.zip”);
final ZipOutputStream zos=newzipoutpstream(newfileoutputstream(“diceUp.zip”);
对于(枚举只需创建名为entryIn object的新ZipEntry对象,并将此新对象放入zos.putNextEntry中即可。
看看这个!
这是我的代码:
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();
}
publicstaticvoidmain(字符串[]args)引发IOException{
ZipFile-ZipFile=new-ZipFile(“/home/***********/resources/dice.zip”);
//ZipFile ZipFile=新ZipFile();
final ZipOutputStream zos=newzipoutpstream(newfileoutputstream(“diceUp.zip”);
谢谢你!我已经挣扎了好几个小时了