Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes Splitting a zip file and rejoining it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Splitting a zip file and rejoining it" Watch "Splitting a zip file and rejoining it" New topic
Author

Splitting a zip file and rejoining it

shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Hi all,

I am developing an application into which i need to split a huge zip file into 4 to 5 parts and then rejoin it...

I have tried this with the code i have attached...

The splitting part works perfectly.. Even the re joining part works but the rejoined file is of 0kb with no content into it...

Any of you free then please execute this code snippet and let me know if you have any solution...

Thanks in Advance..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Lines 114 and 130 cause your loop to break off immediately. You're join method looks for files without the ".sp" extension. If you use line 111 instead of line 112 the program actually runs.

You may want to consider different ways of reading and writing though. Your split takes 6 seconds for a 801kB file with chunk size of 100kB. Your join of the same chunks then takes 301 seconds!

Instead of using RandomAccessFile, try using FileInputStream and FileOutputStream instead. Here's your join method with streams:
As you see, I only changed the RandomAccessFile references to use (File)OutputStream and (File)InputStream instead. I've gone down to 6 seconds for the join. You can perhaps even improve it a bit more by using buffering; simply change lines 9 and 17 from my code to these:
This is called stream chaining. With this buffering the join method now returns within a second, with the same results. I'm sure that using the same approach for the split method will have similar speed improvements.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Hi Rob,

Thanks for your prompt reply...

Thanks for make it time efficient...

Lines 114 and 130 cause your loop to break off immediately. You're join method looks for files without the ".sp" extension. If you use line 111 instead of line 112 the program actually runs.


sorry for this actually i had used line 111 itself...

Even joining is working but the thing is the joined file shows as 0kb and when i use win zip to open it.. It opens and works fine only until i keep the source file i mean the original file in the same location if i delete the source file then it doesn't work properly..

In short it gets redirected to the source file.. Any idea..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Your output file remains as 0 bytes until your program closes because you never flush() your changes. Once you close the file it gets flushed automatically, and the file will have the proper size. But like I said, my measurements with just a small file already took me 5 minutes to join. I've also shown you how to improve that speed.
shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Hi Rob,

I have even tried it with flushing the file...

As per my knowledge flushing doesn't guarantees that the content is written to the physical disk...

I made this code time efficient, but the problem of output file of 0kb still remains the same..

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Splitting a zip file and rejoining it
 
Similar Threads
using javax API to send file contents to COM1
File encryption / decryption retaining line feeds
Splitting a .gz file into specified file sizes
Is there any free libraries avaialble for file splitting usiong java??
how to search for string in file