0

I am writing a client-server program in JAVA in which I am sending a file from server to client.As the file size may be quite high therefore I decided to divide the file in 5 parts and then send it to the same client in 5 different Threads.

  • My Algorithm is to use Java Zip API and create a zip file of the file to be sent,then I will divide the Zip file into 5 parts. The problem is that there is not method in [ZIP API][2] that could divide the file. This is the tutorial that I am referring for sending files through Thread. Anyone who can guide me is there anything wrong with my Algorithm Or do I have to do with different strategy?
  • 3
    Why 5 parts? That seems pretty arbitrary to me. Why not determine a maximum reasonable chunk size, and break it into however many chunks is required? – Jon Skeet Sep 22 '12 at 08:04
  • @JonSkeet that is right,but lets assume for 5..Obviouslt if we can do for 5 ,we can do with any number..The main problem is ZIP API Stuf.. – user1690523 Sep 22 '12 at 08:10
  • It seems to me you should split file on a appropriate number of chunks, then zip and send each chunk separately in serial order, in a single thread. – Mikhail Selivanov Sep 22 '12 at 08:15
  • The idea of using thread is to make it as fast as possible – user1690523 Sep 22 '12 at 08:19

2 Answers2

2

You should separate the zipping part from the splitting part. If you have to send these to a client, you probably don't want to keep the complete zip file in memory while you wait for the client to request the next chunk... so the simplest approach would be to zip to disk first, and then serve that file in chunks. At that point, it really doesn't matter that it's a zip file at all - and indeed for certain files types (e.g. images, sound, video) you may not want to go via a zip file at all.

I would suggest you tell the client the file name and size, and then let the client request whatever section of the file it wants. It can then decide what chunk size to use: you just need to seek to the right bit of the file and serve as much data as the client has requested.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • can I break a normal file which is not text file into chunks? – user1690523 Sep 22 '12 at 08:20
  • @user1690523: Of course. Nothing in my answer suggested anything about text. Indeed, trying to treat it as text and splitting at *character* boundaries is harder. Just think of a file as a bucket of bytes. – Jon Skeet Sep 22 '12 at 08:21
  • but the functions like length(),they operate on a text file and return you number fo characters,How they will operate on a non text file e-g a MP3 file.. – user1690523 Sep 22 '12 at 08:28
  • @user1690523: No they don't. `String.length()` gives you the number of characters in a *string*, but any file operation (e.g. `File.length()`) will give you a length in *bytes*. – Jon Skeet Sep 22 '12 at 08:30
0

Breaking up the file isn't a ZIP function. You could create multiple byte arrays from the resulting zip file (by segmenting the array) and sending each segment in a different thread. This would be similar to what download managers of yesteryear would do.

The client would then have code to re-assemble the byte array in the correct order. You'd probably need to add some additional information to each segment like the correct sequence, the filename to be restored, and the number of segments expected.

KappaMax
  • 324
  • 2
  • 10
  • There's alot of code examples out there (e.g http://www.exampledepot.com/egs/java.io/file2bytearray.html) to read a file into a byte array, but you can make your `byte[]` any size you want it to be, and just read that much data into it. I'd probably create some headers to add to the front of the array with the info I mentioned above. – KappaMax Sep 22 '12 at 08:29