Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RandomAccessFile from ZipEntry (java)

Tags:

java

zip

I was looking for something about reading zip-archives via RandomAccessFile. So, I found this example: http://www.java2s.com/Code/JavaAPI/java.io/RandomAccessFilereadLine.htm However it doesn't work for me, it tells that there's no such file or directory, but the file-path is right. Is this example incorrect?

UPDATE: from docs.oracle.com:

RandomAccessFile(String name, String mode)

Creates a random access file stream to read from, and optionally to write to, a file with the specified name.

It's weird that they try to create RAF with entryName as a "name" parameter in this example

There's one more example with the same thing: http://www.java-tips.org/java-se-tips/java.util.zip/how-to-read-files-within-a-zip-file-3.html

like image 233
user1049280 Avatar asked Mar 15 '26 12:03

user1049280


1 Answers

I think this is a case where un-vetted code winds up on the internets and causes no end of problems.

There is no way the code in those two examples is going to do anything useful. The only way that code would do anything is if the contents of the zip file had already been extracted into the folder that contains the zip.

Long and short: you can't use RAF with ZipEntry because the ZipEntry refers to a compressed stream. You can't do random access on a stream (unless you decompress the whole thing and buffer the results).

It's really interesting to me how:

a) the code in the java-tips article doesn't follow proper naming conventions for Java b) the code in both articles is astoundingly similar

Here's sample code that shows how to properly use ZipInputStream

like image 115
Kevin Day Avatar answered Mar 17 '26 02:03

Kevin Day



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!