Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URGENT: Transferring Zip Files

 
Sanjit Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sanjit Singh
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It works now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic