C# 解压文件时出错

C# 解压文件时出错,c#,xml,C#,Xml,我正在使用来自的SharpZipLib开源.net库 我的目标是解压缩xml文件并将其读入数据集。但是,将文件读入数据集中时会出现以下错误:“根级别的数据无效。第1行,位置1。” 我相信解压代码不会释放文件,原因如下 1.)如果我解压缩文件并退出应用程序。当我重新启动应用程序时,我可以将解压后的文件读入数据集。 2.)如果我在写入xml文件后立即读入该文件(无压缩),则该文件工作正常。 3.)如果我将数据集写入xml,将其压缩、解压,然后尝试将其读回,我会得到异常 下面的代码非常简单。解压文件将

我正在使用来自的SharpZipLib开源.net库

我的目标是解压缩xml文件并将其读入数据集。但是,将文件读入数据集中时会出现以下错误:“根级别的数据无效。第1行,位置1。” 我相信解压代码不会释放文件,原因如下

1.)如果我解压缩文件并退出应用程序。当我重新启动应用程序时,我可以将解压后的文件读入数据集。 2.)如果我在写入xml文件后立即读入该文件(无压缩),则该文件工作正常。
3.)如果我将数据集写入xml,将其压缩、解压,然后尝试将其读回,我会得到异常

下面的代码非常简单。解压文件将返回刚解压的文件名。此调用的正下方是将其读入数据集的调用。变量fileToRead是新解压缩的xml文件的完整路径

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);
    }

那么,最终的文件是什么样子的呢?(与原件相比)。您不显示压缩代码,这可能是拼图的一部分,特别是当您部分吞咽异常时

我还将尝试确保所有的
IDisposable
都是
Dispose()
d,最好是通过
使用
;另外-如果路径构造有问题,请使用
path.Combine
。请注意,如果
myEntry.Name
包含子目录,则需要手动创建它们

以下是我所拥有的-它用于解压缩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);
    }

问题还可能出在编写zip的代码中,或者是读取生成的文件的代码中。

那么,最终的文件是什么样子的呢?(与原件相比)。您不显示压缩代码,这可能是拼图的一部分,特别是当您部分吞咽异常时

我还将尝试确保所有的
IDisposable
都是
Dispose()
d,最好是通过
使用
;另外-如果路径构造有问题,请使用
path.Combine
。请注意,如果
myEntry.Name
包含子目录,则需要手动创建它们

以下是我所拥有的-它用于解压缩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);
    }

问题也可能出在写zip的代码或读取生成文件的代码中。

我使用TextPad将原始文件与最终文件进行了比较,结果完全相同。 此外,我还重写了代码以利用使用。这是代码。 我的问题似乎集中在文件锁定或其他方面。如果我解压缩文件,退出应用程序,然后启动它,它将读取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);
    }

我用TextPad将原稿和最终稿进行了比较,它们是相同的。 此外,我还重写了代码以利用使用。这是代码。 我的问题似乎集中在文件锁定或其他方面。如果我解压缩文件,退出应用程序,然后启动它,它将读取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);
    }

使用DotNetZip要简单得多

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory);       
}
如果要决定提取哪些文件

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
    foreach (ZipEntry e in zip)
    {
        if (wantThisFile(e.FileName)) e.Extract(TargetDirectory);
    }
}
如果要在提取期间覆盖现有文件:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently);
}
或者,要提取受密码保护的条目:

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

使用DotNetZip要简单得多

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory);       
}
如果要决定提取哪些文件

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
    foreach (ZipEntry e in zip)
    {
        if (wantThisFile(e.FileName)) e.Extract(TargetDirectory);
    }
}
如果要在提取期间覆盖现有文件:

using (ZipFile zip = ZipFile.Read(ExistingZipFile))
{
     zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.OverwriteSilently);
}
或者,要提取受密码保护的条目:

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