• Post Reply Bookmark Topic Watch Topic
  • New Topic

Extract a heavy file from JAR

 
Oleg Shubin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to extract some heavy files from my JAR.
I currently use InputStream got via getClass().getResourceAsStream(...) and copy its contents to a FileOutputStream, but this results in very, very low perfomance. I used to use the same method for moving files and it resulted in about a minute per file; when I replaced that code with Files.move(...) it was done in less then a second. I suppose the boost in perfomance is the result of OS handling the operation rather then using Java. Is there a way to extract a file the same way?
Files.copy(...) seems like a possible solution but I don't know whether a java.io.URL of a file inside the JAR can be converted into a java.nio.Path and, if so, whether it would make sence.

The code I currently use:
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I feel the performance problem is because you are reading from disk and writing to disk byte by byte, instead of buffering both in memory.
Wrap the inputstream and fileoutputstream in a BufferedInputStream and BufferedOutputStream of some buffer size. High buffer sizes are fine - modern hardware and OSes can handle them. Increase the buffer sizes till you start seeing a dip in performance again.
 
Oleg Shubin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karthik, thank you a lot!
The problem is gone now, it takes only a second or so to extract 8KB of data (2KB buffer). Taking into account that in the final result I had to extract over 40KB of data it was really critical.
Thanks once more.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oleg Shubin wrote:Files.copy(...) seems like a possible solution but I don't know whether a java.io.URL of a file inside the JAR can be converted into a java.nio.Path and, if so, whether it would make sence.

No, you cannot (easily) turn a URL into a Path. However, Files.copy is overloaded to not only copy from Path to Path, but also from Path to OutputStream or from InputStream to Path. The latter would be useful for you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!