简体   繁体   English

C#只读取第一行,使用压缩文本文件的StreamReader

[英]C# Read only first line, using StreamReader of a zipped text file

I am trying to read only the first line of a zipped csv file. 我试图只读取压缩的csv文件的第一行。 I used below code but get the error "The magic number in GZIP header is not correct". 我使用下面的代码,但得到错误“GZIP标头中的幻数不正确”。 Obviously it has to do with the fact that GZIP and ZIP are not identical formats but I do not seem to get it working even when using the DotNetZipLib library or SharpZip. 显然它与GZIP和ZIP不是相同的格式这一事实有关,但即使使用DotNetZipLib库或SharpZip,我似乎也没有工作。

using (GZipStream gzipStream = new GZipStream(File.OpenRead(fileName), CompressionMode.Decompress))
            {
                using(StreamReader sr = new StreamReader(gzipStream))
                {
                    //Matt try something like this as a hint / starting point 
                    While(sr.Read())
                    {
                      row = sr.ReadLine();
                    }

                }
            }

Does any of you know how to handle standard zip files (not gzip) and to stream the content to a StreamReader object so that I can easily read the first line of the zipped text file? 你们中的任何人都知道如何处理标准的zip文件(而不是gzip)并将内容流式传输到StreamReader对象,以便我可以轻松读取压缩文本文件的第一行吗? I do not look for a solution that completely decompresses the whole zip file before opening the text file. 在打开文本文件之前,我不寻找完全解压缩整个zip文件的解决方案。 I look for a similar solution as above but one that can handle zip files. 我寻找类似于上面的解决方案,但可以处理zip文件。 I also do not want to go the geeky route through byte arrays and having to reconstruct the first row from the array as it would require knowledge of the exact content of the first row (data types, delimiters,...). 我也不想通过字节数组走这条令人讨厌的路线,不得不重新构建数组的第一行,因为它需要知道第一行的确切内容(数据类型,分隔符......)。

Thanks 谢谢

for example Matt here is something that you could do as well checkout this code sample This uses SharpZipLib Library 例如Matt这里是你可以做的事情以及检查这个代码示例这使用SharpZipLib库

var zip = new ZipInputStream(File.OpenRead(@"C:\MyZips\myzip.zip"));
var filestream = new FileStream(@"C:\\MyZips\myzip.zip", FileMode.Open, FileAccess.Read);
ZipFile zipfile = new ZipFile(filestream);
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
     Console.WriteLine(item.Name);
     using (StreamReader s = new StreamReader(zipfile.GetInputStream(item)))
     {
      // stream with the file
          Console.WriteLine(s.ReadToEnd());
     }
 }

The above answer didnt work for me (it casted an error at runtime: nullreference for "item") so i modified the code a bit.(a text file called "text.txt" is zipped in a zip called "archive.zip") This one is in VB.NET and uses SHARPZIPLIB library(you must import it into VB and call it before public class mainform. 上面的答案对我没有用(它在运行时输入了一个错误:“item”的nullreference)所以我修改了一些代码。(一个名为“text.txt”的文本文件压缩成拉链名为“archive.zip”这个是在VB.NET中使用SHARPZIPLIB库(你必须将它导入VB并在公共类mainform之前调用它)。

here is the code : 这是代码:

       Imports ICSharpCode.SharpZipLib.Zip

'now put the following code in a private sub ( i put it in private sub button_click) '现在将以下代码放入私有子(我把它放在私人子按钮_click)

       Dim zip As New ZipInputStream(File.OpenRead("c:\archive.zip")) 'location of the zip file
       Dim filestream As New FileStream("c:\archive.zip", FileMode.Open,FileAccess.Read)
        Dim zipfile As ZipFile = New ZipFile(filestream)

        Dim item As ICSharpCode.SharpZipLib.Zip.ZipEntry
        item = New ZipEntry("text.txt")

        While (Not (zip.GetNextEntry) Is Nothing)
            Console.WriteLine(item.Name)
            Dim s As StreamReader = New StreamReader(zipfile.GetInputStream(item))
            ' stream with the file
            MsgBox(s.Readline)

        End While
        end sub

When u run the code, The message box will popup with the text that is entered in the first line of a text file text.txt Hope this helps. 当你运行代码时,将弹出消息框,其中包含在文本文件text.txt的第一行输入的文本。希望这会有所帮助。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM