Download a zipped file from FTP and extract it?

Jul 2, 2010 at 12:47 AM

Hello All,

            I am trying to download a zipped file sitting on FTP server. This file is a zipped file containing a xml file.

The issue is that I am able to get the zipped file and xml file in it but that xml file is empty.

Can somebody guide me through the correct process.

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://66.97.146.93/Offense_6_24_2010.zip");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential("", "");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

 Ionic.Zip.ZipInputStream zis = new Ionic.Zip.ZipInputStream(responseStream);
            

                    Ionic.Zip.ZipEntry ze = zis.GetNextEntry();

                    //while (ze != null)
                    //{
                    //    MessageBox.Show(ze.FileName);
                    //    ze = zis.GetNextEntry();
                    //}
                    using (Ionic.Zip.ZipFile Zfile =  new Ionic.Zip.ZipFile())
                    {
                        Ionic.Zip.ZipEntry entry = Zfile.AddEntry("test.xml", zis);
                        Zfile.Save(@"C:\New.zip");
                    }

Thank you very much,

Sid

 

Coordinator
Aug 6, 2011 at 1:59 PM

Ah, no - you should not mix the use of ZipInputStream and ZipFile.  They are alternative metaphors for dealing with zip archives.  Choose one or the other, you should almost never use both in the same application.

If you are using ZipInputStream, then you need to use ZipInputStream.GetNextEntry() iteratively, in a loop, in order to enumerate the entries in the zip file and extract them.  Follow the examples for ZipInputStream in the documentation - they all show how do this.

To use ZipFile in this case, you will likely want to save the contents of the FTP response stream into a file, then open the filesystem file with ZipFile.Read().  Follow the documentation.