1

我可以方便地提取 zip 文件中的所有图像文件,但现在如果 zip 文件中存在一些压缩文件,我想提取所有内容。但是当我尝试在压缩文件中提取压缩文件时,我得到“DirectoryNotFoundException”任何帮助都非常感谢。

        List<byte[]> ImagesAsBytes = new List<byte[]>();
    private List<byte[]> FilesToBytesExtarctor(SevenZipExtractor Extractor, String[] FileNames, out String InfoTxt)
    {
        MemoryStream ms = new MemoryStream();
        InfoTxt = "";

        for (int i = 0; i < Extractor.FilesCount; i++)
        {
            if (IsDir(FileNames[i])) continue;
            for (int x = 0; x < SupportedImageFormats.Count; x++)
            {
                if (FileNames[i].ToLower().EndsWith(SupportedImageFormats[x].ToString()))
                {
                    ms = new MemoryStream();
                    Extractor.ExtractFile(FileNames[i], ms); 
                    //Extractor.ExtractArchive(FileNames[i], ms); 
                    ms.Position = 0;
                    ImagesAsBytes.Add(ms.ToArray()); 
                    ms.Close();                      

                }
                else if (FileNames[i].EndsWith(".txt") || FileNames[i].EndsWith(".TXT"))
                {
                    ms = new MemoryStream();
                    Extractor.ExtractFile(FileNames[i], ms);
                    ms.Position = 0;
                    StreamReader sr = new StreamReader(ms);
                    InfoTxt = sr.ReadToEnd();
                    ms.Close();
                    //NextFile = true;
                }
                else if (FileNames[i].ToLower().EndsWith(SupportedArchiveFormats[x].ToString()))
                {
                    SevenZipExtractor Extractor2;
                    string[] files = RawFileExtractor(Path.Combine(Extractor.FileName, FileNames[i]), out Extractor2);
                   ImagesAsBytes.AddRange(FilesToBytesExtarctor(Extractor2, files, out InfoTxt));
                }
            }
        }
        Extractor.Dispose();

        return ImagesAsBytes;
    }

    private String[] RawFileExtractor(string file, out SevenZipExtractor Extractor)
    {
        Extractor = new SevenZipExtractor(file);
        String[] FileNames = Extractor.ArchiveFileNames.ToArray();  
        Array.Sort(FileNames);
        return FileNames;
    }
4

1 回答 1

0

我终于明白了。如果路径包含 zip,则库无法从路径中读取,因此我将内部 zip 文件转换为流,然后使用另一种重载方法递归提取内部 zip 文件的内容。我修改存档路径如下

else if (FileNames[i].ToLower().EndsWith(SupportedArchiveFormats[x].ToString()))
                {
                    ms = new MemoryStream();
                    Extractor.ExtractFile(FileNames[i], ms);
                    ms.Position = 0;


                    SevenZipExtractor Extractor2;
                    string[] files = RawFileExtractorStream(ms, out Extractor2);
                   ImagesAsBytes.AddRange(FilesToBytesExtarctor(Extractor2, files, out InfoTxt)); //recurrsive: call function within its self
                }

然后我为流创建了一个重载方法

    private String[] RawFileExtractorStream(Stream  file, out SevenZipExtractor Extractor)
    {
        Extractor = new SevenZipExtractor(file);
        String[] FileNames = Extractor.ArchiveFileNames.ToArray();  
        Array.Sort(FileNames);
        return FileNames;
    }
于 2013-08-14T07:46:23.110 回答