Unpacking the file

I am using the open source .net library from SharpZipLib from www.icsharpcode.net

My goal is to unpack the xml file and read it into the dataset. However, I get the following error when reading a file in the dataset: "The data at the root level is invalid. Line 1, position 1." I believe what is happening is that the unpacked code does not free the file for the following reasons.

1.) If I unzip the file and exit the application. When I restart the application, I can read the decompressed file into the dataset. 2.) If I read in the xml file right after writing it (no firmware) then it works fine.
3.) If I write a dataset in xml, zip it, unzip it and then try to read it back, I get an exception.

The code below is pretty straightforward. UnZipFile will return the name of the file that was unpacked. It is worth calling right under this call to read it in the dataset. The fileToRead variable is the full path to the new unpacked XML file.

string fileToRead = UnZipFile(filepath, DOViewerUploadStoreArea);
ds.ReadXml(fileToRead )

private string UnZipFile(string file, string dirToUnzipTo)
{

       string unzippedfile = "";

        try
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(file));
            ZipEntry myEntry;
            string tmpEntry = String.Empty;
            while ((myEntry = s.GetNextEntry()) != null)
            {
                string directoryName = dirToUnzipTo;
                string fileName = Path.GetFileName(myEntry.Name);
                string fileWDir = directoryName + fileName;
                unzippedfile = fileWDir;

                FileStream streamWriter = File.Create(fileWDir);
                int size = 4096;
                byte[] data = new byte[4096];
                while (true)
                {
                    size = s.Read(data, 0, data.Length);
                    if (size > 0) { streamWriter.Write(data, 0, size); }
                    else { break; }
                }
                streamWriter.Close();
            }
            s.Close();
        }
        catch (Exception ex)
        {
            LogStatus.WriteErrorLog(ex, "ERROR", "DOViewer.UnZipFile");

        }
        return (unzippedfile);
    }

      

+1


source to share


3 answers


Well, what does the final file look like? (compared to the original). You are not showing the zip code, which could be part of the puzzle, especially since you are partially swallowing the exception.

I have also tried to ensure that everything IDisposable

was Dispose()

d, ideally through using

; also - in case the problem is with path building use Path.Combine

. Please note that if it myEntry.Name

contains subdirectories, you will need to create them manually.

Here's what I have - it works for unpacking ICSharpCode.SharpZipLib.dll:



    private string UnZipFile(string file, string dirToUnzipTo)
    {

        string unzippedfile = "";

        try
        {
            using(Stream inStream = File.OpenRead(file))
            using (ZipInputStream s = new ZipInputStream(inStream))
            {
                ZipEntry myEntry;
                byte[] data = new byte[4096];
                while ((myEntry = s.GetNextEntry()) != null)
                {
                    string fileWDir = Path.Combine(dirToUnzipTo, myEntry.Name);
                    string dir = Path.GetDirectoryName(fileWDir);
                    // note only supports a single level of sub-directories...
                    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                    unzippedfile = fileWDir; // note; returns last file if multiple

                    using (FileStream outStream = File.Create(fileWDir))
                    {
                        int size;
                        while ((size = s.Read(data, 0, data.Length)) > 0)
                        {
                            outStream.Write(data, 0, size);
                        }
                        outStream.Close();
                    }
                }
                s.Close();
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);

        }
        return (unzippedfile);
    }

      

It could also be that the problem is either the code that writes the zip or the code that reads the generated file.

+1


source


I compared the original with the final use of TextPad and they are identical. I also rewrote the code to take advantage of the use of. Here is the code. My problem seems to be centered around file locking or whatever. If I unzip the file, close the application then run it, it will read find.



private string UnZipFile(string file, string dirToUnzipTo)
    {
        string unzippedfile = "";

        try
        {
            using (ZipInputStream s = new ZipInputStream(File.OpenRead(file)))
            {

                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = dirToUnzipTo;
                    string fileName = Path.GetFileName(theEntry.Name);
                    string fileWDir = directoryName + fileName;
                    unzippedfile = fileWDir;

                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(fileWDir))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            LogStatus.WriteErrorLog(ex, "ERROR", "DOViewer.UnZipFile");

        }
        return (unzippedfile);
    }

      

0


source


This is much easier with DotNetZip.

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory);       
}

      

If you want to decide which files to extract ...

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
    foreach (ZipEntry e in zip)
    {
        if (wantThisFile(e.FileName)) e.Extract(TargetDirectory);
    }
}

      

If you want to overwrite existing files during extraction:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently);
}

      

Or, to extract password-protected entries:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.Password = "Shhhh, Very Secret!";
     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently);
}

      

0


source







All Articles