File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes URGENT: Transferring Zip Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "URGENT: Transferring Zip Files" Watch "URGENT: Transferring Zip Files" New topic
Author

URGENT: Transferring Zip Files

Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Hi Everyone,
I have written a simple client and a server program that attmepts to transfer a zip file (though it can e used to transfer any file). It works perfectly and the transferred zip file is saved successfully on the client machine, but the problem arises when I try to open the saved file again using the "ZipFile" class. The server and client code is as follows:
SERVER
************************************************************************
public class OldFtpServer {

private final int port = 50001;
private ServerSocket mServerSocket = null;

public OldFtpServer() {
try {
mServerSocket = new ServerSocket(port);
} catch(IOException e) {
e.printStackTrace();
return;
}

System.out.println("FTP Server Ready");

File file = new File("C:\\error.zip");

while(true) {
BufferedReader in = null;
Socket clientSocket = null;
BufferedOutputStream out = null;
FileInputStream fin = null;

try {
clientSocket = mServerSocket.accept();
} catch(Exception e) {
e.printStackTrace();
return;
}
System.out.println("-->" + clientSocket.getInetAddress().getHostAddress() + " connected");

try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new BufferedOutputStream(clientSocket.getOutputStream());

String temp = null;
String request = in.readLine();
while(!(temp = in.readLine()).equals("end"))
request = request + temp;

fin = new FileInputStream(file);

byte[] bytes = new byte[1024];

while(fin.read(bytes) != -1) {
out.write(bytes);
out.flush();
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
fin.close();
in.close();
out.close();
clientSocket.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}

}
public static void main(String[] args) {
OldFtpServer mFtpServer = new OldFtpServer();
}
}
**********************************************************************
CLIENT
*********************************************************************
public class OldFtpClient {
private final int port = 50001;
private final String address = "localhost";
public OldFtpClient() {
Socket mSocket = null;
try {
mSocket = new Socket(address, port);
} catch (Exception e) {
e.printStackTrace();
return;
}
PrintWriter out = null;
FileOutputStream fout = null;
BufferedInputStream in = null;
File file = new File("C:\\errorresult.zip");
try {
out = new PrintWriter(new OutputStreamWriter(mSocket.getOutputStream()));
in = new BufferedInputStream(mSocket.getInputStream());
out.println("blabla");
out.println("end");
out.flush();
fout = new FileOutputStream(file);
byte[] bytes = new byte[1024];
while(in.read(bytes) != -1) {
fout.write(bytes);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
fout.close();
out.close();
in.close();
mSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//CHECK TO SEE IF ZIP FILE CAN BE OPENED!!!
//This is where the exception occurrs..Why???
try {
ZipFile zipFile = new ZipFile(file);
zipFile.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
OldFtpClient mFtpClient = new OldFtpClient();
}
}
***********************************************************************
As you can see in the client program, the small check in the end fails and an exception is thrown. I have even tried using ZipInputStream and ZipOutputStream, but all my efforts were in vain.Can somebody please help me?
Thanks and looking forward to some replies!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

This code:

always writes the whole buffer out, even if a particular read() call returned less than a full buffer. This guarantees that there will be garbage at the end of the file. The client makes exactly the same mistake, but on the client the garbage may end up anywhere in the file. So change the loops like the above to look like

and your program should work fine.


[Jess in Action][AskingGoodQuestions]
Sanjit Singh
Greenhorn

Joined: Nov 12, 2001
Posts: 27
Thanks! It works now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URGENT: Transferring Zip Files