Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top