0

I am downloading a few .gz file from an FTP server and un-compressing the file to read the data. I am getting the following error.

java.io.IOException: Corrupt GZIP trailer
      at java.util.zip.GZIPInputStream.readTrailer(GZIPInputStream.java:200)
      at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:92)
      at java.io.FilterInputStream.read(FilterInputStream.java:90)
      at com.omnesys.xdk.ClsXDKRTWeb.UnGunZip(ClsXDKRTWeb.java:961)
      at com.omnesys.xdk.ClsXDKRTWeb.DeCompress(ClsXDKRTWeb.java:857)
      at com.omnesys.xdk.ClsXDKRTWeb.FTPDownloadProcess(ClsXDKRTWeb.java:629)
      at com.omnesys.xdk.ClsXDKRTWeb.ProcessRequestXML(ClsXDKRTWeb.java:460)
      at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72)

The code for the fTP download and un compressing is as follows.

    FTPClient ftp;
    FTPClientConfig config;
    ftp = new FTPClient();
    config = new FTPClientConfig();
    ftp.configure(config);
    ftp.connect(strFTPServername);

    ftp.user(strFTPUserName);
    ftp.pass(strFTPUserPwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    OutputStream local = new BufferedOutputStream(new FileOutputStream(strCmnDwnldPath));
    ftp.retrieveFile(strSrcFilePath, local);
    local.close();
    if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
        ftp.disconnect();
        return false;
    } else {
        ftp.logout()
    }


    private boolean UnGunZip(String filename, String outputFolder) {
        byte[] buffer = new byte[1024];
        try {
            String sfilename = new File(filename).getName();
            sfilename = sfilename.substring(0, sfilename.indexOf(".gz"));
            FileInputStream fileIn = new FileInputStream(filename);
            GZIPInputStream gZIPInputStream = new GZIPInputStream(fileIn);
            FileOutputStream fileOutputStream = new FileOutputStream(outputFolder + File.separator + sfilename);
            int count;
            while ((count = gZIPInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, count);
            }
            gZIPInputStream.close();
            fileOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

My application runs in Linux environment. When i try to extract the file in the windows environment i get the error saying the file is broken.

When I try to download the same file from windows environment I don't face this issue.

Can someone help me fix this.

[EDIT:] i found this question, according to this the file should be uploaded as ASCII and downloaded as ASCII. But how do i find out if the file was uploaded using ASCII transfer?

Community
  • 1
  • 1
Ace
  • 700
  • 7
  • 37

1 Answers1

0

Try to remove the "BufferedOutputStream"

OutputStream local = new BufferedOutputStream(new FileOutputStream(strCmnDwnldPath));

This should be enough:

OutputStream local = new FileOutputStream(strCmnDwnldPath);
ozma
  • 1,633
  • 1
  • 20
  • 28
  • 1
    Do you think this is an answer and will solve the problem? – Garry Aug 18 '15 at 13:12
  • I haven't tried it on both environments (linux and windows) but it should fix it. BufferedOutputStream might replece some "\n" to "\n\r" or vice versa. In binary file it is critical. – ozma Aug 18 '15 at 13:28
  • @ozma Issue still exists. – Ace Aug 19 '15 at 07:34
  • I am Sorry. According to your "edit" I think of another thing. First: upload should also be binary. Second: try to test upload and download separately. That means instead of upload from other java, just copy the file to the ftp dir and then test download. after this works start testing upload. – ozma Aug 19 '15 at 08:00
  • @ozma when i do a upload and then download I don't get the error. – Ace Aug 19 '15 at 12:40
  • So i guess the problem is in the upload code (are you using "BufferdStream" there too?). can you post the upload code too? in binary files the key is not to use buffers that are intended for text since they might change the data (EOL mostly). – ozma Aug 19 '15 at 13:02
  • The original upload is not done by me, and i am not sure how do they do it. The upload that i had done was manual using Filezilla. – Ace Aug 20 '15 at 06:04
  • If upload with filezilla works and upload with some java code fails it probably means that the java code need to be fixed or replaced (nothing in life is certain, that could be filezilla bug but I don't think it is (:) – ozma Aug 20 '15 at 06:17