本文介绍了解压文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我使用SharpZipLib的开源.NET库从



我的目标是要解压缩XML文件,读入的数据集。不过,我得到以下错误文件读入数据集:在根级别的数据无效1号线,位置1
我相信所发生的事情是解压代码没有释放原因如下文件。



1)。如果我解压缩文件,并退出应用程序。当我重新启动应用程序我可以读取该文件解压缩到一个数据集。
2)如果我在xml文件中写出来(无压缩和解)之后读那么它工作正常。

3)如果我写的数据为XML,压缩起来,将它解压缩,然后尝试读回我得到的异常。



下面的代码是非常直截了当。 UnZipFile将返回刚才解压缩的文件的名称。下方这个调用读入数据集呼叫。该变量fileToRead是将新解压缩XML文件的完整路径。



 字符串fileToRead = UnZipFile(文件路径,DOViewerUploadStoreArea); 
ds.ReadXml(fileToRead)

私人字符串UnZipFile(字符串文件,字符串dirToUnzipTo)
{

串unzippedfile =;


{
ZipInputStream S =新的ZipInputStream(File.OpenRead(文件));
的ZipEntry myEntry;
串tmpEntry =的String.Empty;
,而((myEntry = s.GetNextEntry())!= NULL)
{
字符串目录名= dirToUnzipTo;
字符串文件名= Path.GetFileName(myEntry.Name);
串fileWDir =目录名文件名+;
unzippedfile = fileWDir;

的FileStream的StreamWriter = File.Create(fileWDir);
INT大小= 4096;
字节[]数据=新的字节[4096];
,而(真)
{
尺寸= s.Read(数据,0,data.Length);
如果(大小大于0){streamWriter.Write(数据,0,大小); }
,否则{打破; }
}
streamWriter.Close();
}
S.CLOSE();
}
赶上(异常前)
{
LogStatus.WriteErrorLog(例如,ERROR,DOViewer.UnZipFile);

}
回报(unzippedfile);
}


解决方案

那么,做最后的文件是什么样子? (比较原始)。你不显示荏苒代码,这可能是问题的一部分,尤其是当你被部分吞咽异常。



我也想尝试一切确保的IDisposable 的Dispose() D,最好通过使用;也 - 因问题与路径构造,使用 Path.Combine 。请注意,如果 myEntry.Name 包含子目录,则需要手动创建它们。



下面就是我有 - 它为解压缩ICSharpCode.SharpZipLib.dll:

 私人字符串UnZipFile(字符串文件,字符串dirToUnzipTo)
{

串unzippedfile =;


{使用使用(流inStream = File.OpenRead(文件))
(ZipInputStream S =新的ZipInputStream(插播广告))
{

的ZipEntry myEntry;
字节[]数据=新的字节[4096];
,而((myEntry = s.GetNextEntry())!= NULL)
{
串fileWDir = Path.Combine(dirToUnzipTo,myEntry.Name);
串DIR = Path.GetDirectoryName(fileWDir);
//注意只支持子目录的单级...
如果Directory.CreateDirectory(DIR)(Directory.Exists(DIR)!);
unzippedfile = fileWDir; // 注意;如果返回最后一个文件使用多个

(的FileStream outStream = File.Create(fileWDir))
{
INT大小;
而((大小= s.Read(数据,0,data.Length))大于0)
{
outStream.Write(数据,0,大小);
}
outStream.Close();
}
}
S.CLOSE();
}

}
赶上(异常前)
{
Console.WriteLine(除息);

}
回报(unzippedfile);
}



这也可以,问题是无论是在写入的zip代码或读取所生成的文件中的代码


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

My goal is to unzip an xml file and read it into a dataset. However I get the following error reading the file into a dataset: "Data at the root level is invalid. Line 1, position 1."I believe what is happening is the unzipping code is not releasing the file for the following reasons.

1.) If I unzip the file and exit the application. When I restart the app I CAN read the unzipped file into a dataset.2.) If I read in the xml file right after writing it out (no zipping) then it works fine.
3.) If I write the dataset to xml, zip it up, unzip it, then attempt to read it back in I get the exception.

The code below is pretty straight forward. UnZipFile will return the name of the file just unzipped. Right below this call is the call to read it into a dataset. The variable fileToRead is the full path to the newly unzipped 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);
    }
解决方案

Well, what does the final file look like? (compared to the original). You don't show the zipping code, which might be part of the puzzle, especially as you are partially swallowing the exception.

I would also try ensuring everything IDisposable is Dispose()d, ideally via using; also - in case the problem is with path construction, use Path.Combine. And note that if myEntry.Name contains sub-directories, you will need to create them manually.

Here's what I have - it works for unzipping 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 in the code that writes the zip, or the code that reads the generated file.

这篇关于解压文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:26