I'm using the Java implementation of SSH, JSCH to do an scp from remote to local. Following the JSCH example code to scpfrom I'm able to receive files. I've used the same implementation as in the above link.
Problem: Compressed files/photos sent are corrupt. Text files appear the same. However, when I send a binary (like a .tgz), the decompresser complains the file is corrupt. I suspect this is a binary vs ascii issue. Is this because of the way I am writing the data to file in Java? How do I properly receive and write binary files using JSCH?
The code I've written -
private void executeCommand(String username, String pwd, String hostname, int port)
throws JSchException, IOException {
JSch jSch = new JSch();
Session session = jSch.getSession(username, hostname, port);
session.setPassword(pwd);
Properties properties = new Properties();
properties.put("StrictHostKeyChecking", "no");
session.setConfig(properties);
session.connect();
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("scp -f /home/rish/haha.txt");
OutputStream outputStream = channelExec.getOutputStream();
DataInputStream inputStream = new DataInputStream(channelExec.getInputStream());
DataOutputStream fos;
channelExec.connect();
byte[] buf = new byte[1024];
buf[0] = 0;
outputStream.write(buf, 0, 1);
outputStream.flush();
while (true) {
int c = checkAck(inputStream);
if (c != 'C') {
break;
}
// read '0644 '
inputStream.read(buf, 0, 5);
long filesize = 0L;
while (true) {
if (inputStream.read(buf, 0, 1) < 0) {
// error
break;
}
if (buf[0] == ' ') break;
filesize = filesize * 10L + (long) (buf[0] - '0');
}
String file = null;
for (int i = 0; ; i++) {
inputStream.read(buf, i, 1);
if (buf[i] == (byte) 0x0a) {
file = new String(buf, 0, i);
break;
}
}
// send '\0'
buf[0] = 0;
outputStream.write(buf, 0, 1);
outputStream.flush();
// String fileName = "/data/data/" + getPackageName() + "/crearofile" + new Random().nextInt(100) + ".txt";
String fileName = "/data/data/rish.crearo.trial/haha.txt";
File file1 = new File(fileName);
if (file1.exists()) file1.delete();
if (file1.createNewFile()) Log.d(TAG, "File created");
else Log.d(TAG, "File not created");
// read a content of lfile
fos = new DataOutputStream(new FileOutputStream(fileName));
String count;
while ((count = inputStream.readUTF()) != null) {
System.out.println(count);
fos.writeBytes(count);
}
fos.close();
if (checkAck(inputStream) != 0) {
System.exit(0);
}
inputStream.close();
// send '\0'
buf[0] = 0;
outputStream.write(buf, 0, 1);
outputStream.flush();
}
session.disconnect();
}