Question

(First of all I'm new here and my english is not very good, so I want to apologize for my bad writting skills)

I'm working on a Android proyect which reads from a public FTP server some .zip files and get the content from them. I'm connecting to the ftp using the org.apache.commons.net.ftp library.

I can list all the .zip files from the server, but when I try to list the content I start having problems. Some files are fine, but in some cases I can't read the whole zip content.

This is part of the code I'm using to connect to the ftp:

private static final String TAG = "MyFTPClient";
public FTPClient mFTPClient = null; 

//Method to connect to FTP server:
public boolean ftpConnect(String host, String username, String password, int port) {
    try {
        mFTPClient = new FTPClient();        
        // connecting to the host
        mFTPClient.connect(host);
        // now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
            // login using username & password
            boolean status = mFTPClient.login(username, password);
            /* Set File Transfer Mode
             *
             * To avoid corruption issue you must specified a correct
             * transfer mode, such as ASCII_FILE_TYPE, BINARY_FILE_TYPE,
             * EBCDIC_FILE_TYPE .etc. Here, I use BINARY_FILE_TYPE
             * for transferring text, image, and compressed files.
            */
            mFTPClient.setFileType(FTP.ASCII_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            return status;

        }
    } catch(Exception e) {
        Log.d(TAG, "Error: could not connect to host " + host );
        Log.d(TAG, e.toString());
    }

    return false;
} 

And this is the code I'm using to list the name of the file from the .zips:

    public ArrayList<String> ftpZipRetrieveFilesTitles(String filePath) throws IOException{
    ZipInputStream zis = null;
    ArrayList<String> fitxIzenak = new ArrayList<String>();
    try {
         zis = new ZipInputStream(mFTPClient.retrieveFileStream(filePath));
         ZipEntry ze;
         while ((ze = zis.getNextEntry()) != null) {
             String filename = ze.getName();
             fitxIzenak.add(filename);
         }                   
         zis.close();
         mFTPClient.completePendingCommand();
         return fitxIzenak;

    } catch (Exception e) {
        zis.close();
        mFTPClient.completePendingCommand();
        e.printStackTrace();
    }

    return fitxIzenak;
}

And this is what I get:

java.util.zip.ZipException: CRC mismatch
    at java.util.zip.ZipInputStream.readAndVerifyDataDescriptor(ZipInputStream.java:208)
    at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:172)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:225)
    at com.example.services.MyFTPClient.ftpZipRetrieveFromFiles(MyFTPClient.java:250)
    at com.example.gipuzkoabidaian.MainActivity$FTPkonexioa.doInBackground(MainActivity.java:965)
    at com.example.gipuzkoabidaian.MainActivity$FTPkonexioa.doInBackground(MainActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)

 java.util.zip.ZipException: data error
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:340)
    at libcore.io.Streams.skipByReading(Streams.java:158)
    at java.util.zip.InflaterInputStream.skip(InflaterInputStream.java:217)
    at libcore.io.Streams.skipAll(Streams.java:133)
    at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:152)
    at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:225)
    at com.example.services.MyFTPClient.ftpZipRetrieveFilesTitles(MyFTPClient.java:303)
    at com.example.gipuzkoabidaian.MainActivity$FTPkonexioa.doInBackground(MainActivity.java:961)
    at com.example.gipuzkoabidaian.MainActivity$FTPkonexioa.doInBackground(MainActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)

I hope anyone can help me, I don't know what to do.

Was it helpful?

Solution

In the end I've found the solution by myself. (I not sure if I have to write the answer here...)

The only thing that I've changed is the file type to transfer in "ftpconnect" function:

mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);

I'm not very sure about the reason, anyway, I found here some explanation: FTP Client - setFileType

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