2

I am trying to download a tar.gz file from a FTP Server (over HTTP Proxy). While the hastle of connecting over the proxy has been taken care of (I am able to read regular files from FTP site), I am able to download file to a my desktop (obviously a developer machine). But when I try inflate that file I am getting an exception. Below are my code snippets

if (CollectionUtils.isNotEmpty(filesList)) {
    for (String fileName : filesList) {
        if (fileName.endsWith(getArchiveFileType())) {
            // File is a tar.gz file.
            // Download this file to a local directory
            FtpDownloadMethod downloadMethod = new FtpDownloadMethod();
            OutputStream output = new FileOutputStream(getOutputDirectory() + fileName);
            downloadMethod.setFileTransferMode(FTPClient.COMPRESSED_TRANSFER_MODE);
            downloadMethod.setOutput(output);
            downloadMethod.setUri(fileName);
            isDownloaded = isDownloaded && getServiceProxy().executeMethod(downloadMethod).isSuccess();
            output.close();
        }
    }
}

Here is the actual method that does the transfer.

String uri; //method arguements
OutputStream output; //method arguements

DefaultFtpResponse response = null;

try {
    response = new DefaultFtpResponse();

    boolean success = false;

    if (StringUtils.isBlank(getUri())) {
        response.setStatusCode(FTPReply.FILE_UNAVAILABLE);
    } else {
        aFtpClient.connect();
        aFtpClient.enterLocalPassiveMode();
        aFtpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
        aFtpClient.setFileTransferMode(getFileTransferMode());
        success = aFtpClient.retrieveFile(uri, output);
        response.setSuccess(success);
        response.setStatusCode(FTPReply.COMMAND_OK);
    }
} catch (Exception exp) {
    LOGGER.error(exp);
}

return response;

This code creates a file and i can see a file being created in the desired output folder. After downloading, when i try to unzip the downloaded file, i see this error.

    java.util.zip.ZipException: oversubscribed literal/length tree

I tried BLOCK_TRANSFERR_MODE AND BINARY_TRANSFER_MODE as well.

higuaro
  • 15,730
  • 4
  • 36
  • 43
Sriram
  • 71
  • 1
  • 7

2 Answers2

2

The uploaded file was corrupt. I was trying to upload in binary transfer mode. After changing it to Compressed transfer mode, I am able to process the file.

Sriram
  • 71
  • 1
  • 7
0

May I know how do you "unzip" the downloaded file? As you mentioned the file downloaded is .tar.gz, which is not in zip format, so I think you are trying to extrace the .tar.gz as a .zip file, which should not work. If you want to extract .tar.gz file, you may refer to this question: How do I extract a tar file in Java?

Community
  • 1
  • 1
Raymond Tau
  • 3,429
  • 26
  • 28
  • That is exactly what I am doing. Infact, I referred to the same post prior to doing this. As I might try, I tried unziping the tar.gz file with WinZip. Even with that, I was not able to open the gzip file. On the same note, I opened the tar.gz file with WinZip before uploading to the FTP site. I suspected upload might have corrupted the file. I reloaded the file in Binary format using the same FTP client instance. – Sriram Nov 13 '12 at 03:31
  • @Sriram Did you try to save the file to disk, compare that with the one you uploaded? – Raymond Tau Nov 13 '12 at 06:17
  • Thanks Raymond. The file that is initially uploaded was corrupt. Re-uploading in Compressed mode did the magic. – Sriram Nov 13 '12 at 23:16