Unzip and save .csv but when open appears unreadable

I am unzipping a zipped csvfile and saving it to a path.  It does save but when I open it later for processing, it is unreadable.   I've included my code - may be a simple correction or addition to code here but I'm hung up!  Also, am I setting the size incorrectly - not sure on this either.   Fastest solution/fix to my code that works gets points.  [I need this to save the unzipped file.csv such that when opened programmaticaly it is readable]

        public static string UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile, int FileNum)
            ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));

            string sReturnFileToProcess = "";
            string TempUnZipFolder = "";
            string sFileNamed = "";

            TempUnZipFolder = @"\\\\server\\driveletter$\\folder\\anotherfolder\\";

            if (password != null && password != String.Empty)
                s.Password = password;
            ZipEntry theEntry;
            string tmpEntry = String.Empty;
            while ((theEntry = s.GetNextEntry()) != null)

                // string directoryName = outputFolder;
                string directoryName = TempUnZipFolder;
                string fileName = Path.GetFileName(theEntry.Name);

                // create directory
                if (directoryName != "")
                if (fileName != String.Empty)
                    if (theEntry.Name.IndexOf(".ini") < 0)
                        string fullPath = TempUnZipFolder + "\\" + FileNum.ToString() + "_" + theEntry.Name;
                        sFileNamed = FileNum.ToString() + "_" + theEntry.Name;

                        sReturnFileToProcess = sFileNamed;

                        fullPath = fullPath.Replace("\\ ", "\\");
                        string fullDirPath = Path.GetDirectoryName(fullPath);
                        if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                        FileStream streamWriter = File.Create(fullPath);
                        int size = 20480;

                        byte[] data = new byte[20480];

                        while (true)
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                                streamWriter.Write(data, 0, size);
            if (deleteZipFile)

            return sReturnFileToProcess;


Thank in advance!
Who is Participating?
b_levittConnect With a Mentor Commented:
I'm unfamiliar with the zip component you're using, but if you can manually unzip this file, than I would start with that components documentation.  I've always had good luck with dotnetzip:
Jaime OlivaresSoftware ArchitectCommented:
at a first glance your code looks well. How are you verifying that the file is unreadable?
Shay10Author Commented:
I open the same file (that is unzipped in the code I've posted) later in processing using a different method I wrote.  This same method works fine on .csv files that have been manually extracted from zip.  

The files I have unzipped in code behind are simply scrambled when this method opens them and attempts to read line by line.  That's the pattern - anything unzipped in code fails while a normal (manually extracted .csv file) reads fine.  

I'm out of time tonight - will check out the dotnetzip link soon.  Any other questions, please post it as I'll be checking this atleast twice a day.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

AndyAinscowFreelance programmer / ConsultantCommented:
Can you post a bit of the 'garbled' text you get after unzipping ?
I'm wondering if you are somehow getting a UNICODE/ANSI scrambling
Shay10Author Commented:
This is some of what a line looks like in code.


-not sure what I'm missing.
Shay10Author Commented:
Was able to make this work quickly - still not sure why other code snippet was failing.  Might revisit that later if time permits.

Thanks for the link - was very helpful!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.