File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Open Source Projects and the fly likes Ftp corrupts the tar.gz file while copying the file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark " Ftp corrupts the tar.gz file while copying the file." Watch " Ftp corrupts the tar.gz file while copying the file." New topic
Author Ftp corrupts the tar.gz file while copying the file.

Andy Saxena

Joined: Aug 09, 2012
Posts: 4

I am in a situation where my ftp code copies the tar.gz file from the remote location. But that file is somehow corrupted. Mu unzip code in java fails, also, when I manually try to unzip the file in windows using WinRAR, it also says that the package is corrupted. Whereas the file in server is perfect.

I tried using Apache Commons Net FTP jar as well. But there also the same problem.

Below is my FTP code :

FileTransferClient ftp = null;
String fileName = "";
// create client"\nCreating FTP client");
ftp = new FileTransferClient();
try {
} catch (Exception e) {
System.out.println("ERROR: ");
System.out.println("Please Check the Following :");
System.out.println("-> Server Name or IP Address");
System.out.println("-> User Name");
System.out.println("-> Password for <User Name> Account");
System.out.println("ERROR: Could not connect to server. DBX will now exit..");
}"Connected and logged in to server " + serverName);
int index = remoteDir.lastIndexOf("/");
String parentRemoteDir = remoteDir.substring(0, index);
fileName = remoteDir.substring(index + 1);


FTPFile[] files = ftp.directoryList(".");
if (loadMYSQLArchive) {

// get MYSQL Archives
boolean localDbxArchiveExists = (new File(localDir)).exists();
if (!localDbxArchiveExists) {
boolean createLocalDbxArchive = (new File(localDir)).mkdir();
if (createLocalDbxArchive) {
} else {
boolean createLocalDbxArchive = (new File(localDir)).mkdir();

if (createLocalDbxArchive) {"Local directory created at \"" + localDir + "\"");
int dwnldedFiles = 0;
for (int i = 0; i < files.length; i++) {
if (!files[i].isDir() && files[i].getName().equalsIgnoreCase(fileName)) {"Downloading file from \"" + files[i].getName() + " To location \"" + localDir + File.separator
+ files[i].getName() + "\"");
FileTransferInputStream downloadedStream = ftp.downloadStream(files[i].getName());
final FileOutputStream out = new FileOutputStream(localDir + File.separator + files[i].getName());
byte[] bytes = new byte[256];
int len;
while ((len = > 0) {
out.write(bytes, 0, len);

// ftp.downloadFile(localDir + File.separator + files[i].getName(), files[i].getName());
} else {"Skipping directory " + files[i].getName());
if (dwnldedFiles == 0) {
throw new Exception("DB archive not found, please check the archive path.");


Please help me with this issue.

I agree. Here's the link:
subject: Ftp corrupts the tar.gz file while copying the file.
It's not a secret anymore!