Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ZipException - invalid entry compressed size

 
Brian Taylor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm trying to download a .zip file from a server via FTP and write it to the local server's file system. When I run this code,I get this Exception:
java.util.zip.ZipException: invalid entry compressed size (expected 264052 but got 261402 bytes)
It gets thrown when zout.closeEntry() is called.
I have been able to get the file to download, but it is about 3k smaller than if I manually downloaded it and WinZip will not recognize it as an appropriate zip format.
Has anyone had this problem? I included a portion of the code below.
Thanks in advance,
Brian
------------
public void run() {
URL url = buildURL();
BufferedReader br = null;
ZipInputStream zin = null;
ZipOutputStream zout = null;
File zipFileDir = new File("C:\\temp");
String zipFile = "s111601.zip";
try{
// -- get historical stock .zip:
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream();
zin = new ZipInputStream(url.openStream());
zout = new ZipOutputStream(new FileOutputStream(zipFile));
zout.putNextEntry(zin.getNextEntry());
byte[] buf = new byte[1024];
int len;
while ((len = zin.read(buf)) > 0) {
zout.write(buf, 0, len);
}
zout.closeEntry();
zout.close();
}
catch(Exception e) {
System.out.println("("+e.toString()+") caught in run()");
}
br = null;
url = null;
}
 
Art Walker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,
I was having the same problem you described. The fix I found was to create a new ZipEntry object for the destination file that only uses the name of the ZipEntry object obtained from the source. In other words, try changing your code that sets the ZipEntry from this:
(1) zout.putNextEntry(zin.getNextEntry());
to this:
(2) ZipEntry destEntry =
new ZipEntry (zin.getName());
zout.putNextEntry(destEntry);
It's interesting to note that every example that I was able to find on-line that performed a zip-to-zip copy used the technique found on line (1) above. This seems to work fine for ZIP files containing relatively small files, but resulted in "java.util.zip.ZipException: invalid entry compressed size" exceptions if the ZIP file contained any moderate or large-sized files.
Art
 
Art Walker
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops -- the code for (2) should've read
(2) ZipEntry destEntry =
new ZipEntry (zin.getNextEntry().getName());
zout.putNextEntry(destEntry);
Sorry for the goof!
Art
 
Arturo Gonzalez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art Walker wrote:Oops -- the code for (2) should've read
(2) ZipEntry destEntry =
new ZipEntry (zin.getNextEntry().getName());
zout.putNextEntry(destEntry);
Sorry for the goof!
Art


Thanks man, this code works perfectly, I have been searching that for a week ^^
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic