28035

Error java.util.zip.ZipException: invalid entry size while copying image (.png) from 1 zip file to a

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

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: invalid entry compressed size (expected 113177 but got 113312 bytes)

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(); }

Answer1:

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(); }

Recommend

  • specifying classpath for standalone jython
  • Microsoft Graph API - Read Office 365 SharePoint List and Pull Data (Images)
  • ServiceNow: How to create a workflow that runs on incident update?
  • Setting up cakephp 2.0 on bluehost
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • How do I include a SWC in an AS2 Flash project?
  • How to add a focus style to an editable ComboBox in WPF
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • How do I superscript characters in a UIButton?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • PHP buffered output depending on server setting?
  • copying resource to sdcard gives a damaged file in android
  • Functions in global context
  • Python urlparse: small issue
  • Spark fat jar to run multiple versions on YARN
  • FileReader+canvas image loading problem
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Counter field in MS Access, how to generate?
  • Is possible to count alias result on mysql
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Check if a string to interpolate provides expected placeholders
  • Javascript Callbacks with Object constructor
  • sending/ receiving email in Java
  • How to set my toolbar fixed while scrolling android
  • How can I use Kendo UI with Razor?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal