Solved

Unzip and save .csv but when open appears unreadable

Posted on 2010-11-10
6
536 Views
Last Modified: 2013-12-17
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 != "")
                {
                    Directory.CreateDirectory(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);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
            }
            s.Close();
            if (deleteZipFile)
            {
                File.Delete(zipPathAndFile);
            }

            return sReturnFileToProcess;

        }


Thank in advance!
0
Comment
Question by:Shay10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 34109310
at a first glance your code looks well. How are you verifying that the file is unreadable?
0
 
LVL 11

Accepted Solution

by:
b_levitt earned 500 total points
ID: 34111828
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:
http://dotnetzip.codeplex.com/
0
 

Author Comment

by:Shay10
ID: 34117762
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.
Thanks!

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34120056
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
0
 

Author Comment

by:Shay10
ID: 34141518
This is some of what a line looks like in code.

¿¿¿¿C¿|al¿¿¿4¿8¿S;^¿¿¿8¿¿P¿¿0¿¿¿¿¿¿¿¿¿¿¿¿w¿¿¿¿;\f}¿6NG¿p¿¿xe¿A¿¿]?n¿¿¿¿H¿PiI¿:]U^z¿?¿¿¿¿¿WR

-not sure what I'm missing.
0
 

Author Closing Comment

by:Shay10
ID: 34142486
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!
0

Featured Post

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question