C# Access text file in zip archive

删除回忆录丶 提交于 2019-12-12 20:54:03

问题


How can I read content of a text file inside a zip archive?

For example I have an archive qwe.zip, and insite it there's a file asd.txt, so how can I read contents of that file?

Is it possible to do without extracting the whole archive? Because it need to be done quick, when user clicks a item in a list, to show description of the archive (it needed for plugin system for another program). So extracting a whole archive isn't the best solution... because it might be few Mb, which will take at least few seconds or even more to extract... while only that single file need to be read.


回答1:


You could use a library such as SharpZipLib or DotNetZip to unzip the file and fetch the contents of individual files contained inside. This operation could be performed in-memory and you don't need to store the files into a temporary folder.




回答2:


Unzip to a temp-folder take the file and delete the temp-data

    public static void Decompress(string outputDirectory, string zipFile)
    {
        try
        {
            if (!File.Exists(zipFile))
                throw new FileNotFoundException("Zip file not found.", zipFile);

            Package zipPackage = ZipPackage.Open(zipFile, FileMode.Open, FileAccess.Read);
            foreach (PackagePart part in zipPackage.GetParts())
            {
                string targetFile = outputDirectory + "\\" + part.Uri.ToString().TrimStart('/');
                using (Stream streamSource = part.GetStream(FileMode.Open, FileAccess.Read))
                {
                    using (Stream streamDestination = File.OpenWrite(targetFile))
                    {
                        Byte[] arrBuffer = new byte[10000];
                        int iRead = streamSource.Read(arrBuffer, 0, arrBuffer.Length);
                        while (iRead > 0)
                        {
                            streamDestination.Write(arrBuffer, 0, iRead);
                            iRead = streamSource.Read(arrBuffer, 0, arrBuffer.Length);
                        }
                    }
                }
            }
        }
        catch (Exception)
        {

            throw;
        }
    }



回答3:


Although late in the game and the question is already answered, in hope that this still might be useful for others who find this thread, I would like to add another solution.

Just today I encountered a similar problem when I wanted to check the contents of a ZIP file with C#. Other than NewProger I cannot use a third party library and need to stay within the out-of-the-box .NET classes.

You can use the System.IO.Packaging namespace and use the ZipPackage class. If it is not already included in the assembly, you need to add a reference to WindowsBase.dll.

It seems, however, that this class does not always work with every Zip file. Calling GetParts() may return an empty list although in the QuickWatch window you can find a property called _zipArchive that contains the correct contents.

If this is the case for you, you can use Reflection to get the contents of it. On geissingert.com you can find a blog article ("Getting a list of files from a ZipPackage") that gives a coding example for this.




回答4:


SharpZipLib or DotNetZip may still need to get/read the whole .zip file to unzip a file. Actually, there is still method could make you just extract special file from the .zip file without reading the entire .zip file but just reading small segment.




回答5:


I needed to have insights into Excel files, I did it like so:

using (var zip = ZipFile.Open("ExcelWorkbookWithMacros.xlsm", ZipArchiveMode.Update))
{
    var entry = zip.GetEntry("xl/_rels/workbook.xml.rels");
    if (entry != null)
    {
        var tempFile = Path.GetTempFileName();
        entry.ExtractToFile(tempFile, true);
        var content = File.ReadAllText(tempFile);
        [...]
    }
}


来源:https://stackoverflow.com/questions/8151583/c-sharp-access-text-file-in-zip-archive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!