corrupt file after file transfer

I have written code to transfer two files from the client to the server. Every time I transfer the file and try to decompress it using gzip, it says "corrupt input. Use zcat to recover some data." Is there any way I can fix this? Please help...

Client side:

for (int z = 0; z < files.length; z++){

dos.writeUTF(files[z].getName());

dos.writeLong(files[z].length());

buffer =newbyte[(int)files[z].length()];

dis =new DataInputStream(new BufferedInputStream(new FileInputStream(files[z])));

int len = 0;

while ((len = dis.read(buffer)) > 0){

dos.write(buffer, 0, len);

System.out.print("#");

}

dis.close();

}

dos.flush();

dos.close();

Server side:

for(int i = 0; i<=1; i++){

String a = dis.readUTF();

setFileName(a);

System.out.println(getFileName());

long b = dis.readLong();

System.out.println(b);

buffer =newbyte[(int)b];

dos =new DataOutputStream(new BufferedOutputStream(new FileOutputStream("tmp/" + util.getTmpDir() +"/" +getFileName())));

int len = 0;

int offset = 0;

while(offset < buffer.length && (len=dis.read(buffer, offset, buffer.length-offset)) >= 0){

offset += len;

dos.write(buffer, 0, len);

System.out.print("#");

}

}

[2431 byte] By [Mjacobsa] at [2007-11-27 9:55:02]
# 1

> Server side:

> while(offset < buffer.length && h && (len=dis.read(buffer, offset, buffer.length-offset)) >= 0){

>

> offset += len;

> dos.write(buffer, 0, len);

You're reading into a segment of the buffer that could start in the middle, but then always writing from the beginning of the buffer.

I'd suggest using a relatively small fixed-size buffer (say 8k), rather than trying to allocate a buffer that's the size of your file, especially since (as you've discovered), you probably won't read the entire file at once.

You should probably look at Jakarta Commons IO to see if they have code that will copy your data from input to output: http://jakarta.apache.org/commons/io/apidocs/index.html

kdgregorya at 2007-7-13 0:25:07 > top of Java-index,Java Essentials,Java Programming...