aspose file tools*
The moose likes I/O and Streams and the fly likes ZipException - invalid entry compressed size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "ZipException - invalid entry compressed size" Watch "ZipException - invalid entry compressed size" New topic
Author

ZipException - invalid entry compressed size

Brian Taylor
Greenhorn

Joined: Nov 20, 2001
Posts: 1
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

Joined: May 30, 2002
Posts: 2
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<br />Senior Software Developer<br />Aether Systems, Inc.<br />11460 Cronridge Drive<br />Owings Mills, MD 21117<br />Email: awalker@aethersystems.com
Art Walker
Greenhorn

Joined: May 30, 2002
Posts: 2
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

Joined: Mar 26, 2012
Posts: 2
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 ^^
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ZipException - invalid entry compressed size