1

I can conveniently extract all image files within the zip file, but now if there exist some zipped files within the zip file I want to extract everything. But when I try to extarct the zipped file within the zip file I get "DirectoryNotFoundException" Any help is greatly appreciated.

        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;
    }
electricalbah
  • 2,227
  • 2
  • 22
  • 36
  • which line is giving error? – Ehsan Aug 14 '13 at 04:55
  • @EhsanUllah In the function RawFileExtractor, the line "Extractor = new SevenZipExtractor(file);" gives the error during extraction of zip within zip. But I finally got a head start by extracting the zip file into a stream and using another overload private String[] RawFileExtractorStream(Stream file, out SevenZipExtractor Extractor) – electricalbah Aug 14 '13 at 05:06
  • you should first check whether or not the file even exists – Ehsan Aug 14 '13 at 05:08
  • @EhsanUllah thanks, the file exists but I think when the path contains a zip in the middle, it cant read it eg c:\\mainfile.zip\\subfile.zip – electricalbah Aug 14 '13 at 05:13
  • the path contains zip or the zip contains another zip? – Ehsan Aug 14 '13 at 05:18
  • @EhsanUllah The main zip contains a sub zip file. Therefore the path to the sub zip file will contain a .zip string "c:\\mainfile.zip\\subfile.zip" – electricalbah Aug 14 '13 at 05:27
  • then you will have to write a recursive mechanism that checks that if the file is a zip file then calls your method again – Ehsan Aug 14 '13 at 05:43
  • @EhsanUllah I finally got it. If the path contains a zip, The library cant read from the path, so I converted the inner zip file to stream and then use another overloaded method to extract the content of the inner zip file recurssively. Thanks anyway – electricalbah Aug 14 '13 at 07:41
  • good that your problem is solved. – Ehsan Aug 14 '13 at 07:45

1 Answers1

0

I finally got it. If the path contains a zip, The library cant read from the path, so I converted the inner zip file to stream and then use another overloaded method to extract the content of the inner zip file recurssively. I modified the archive path as follows

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
                }

Then I created an overloaded method for the stream

    private String[] RawFileExtractorStream(Stream  file, out SevenZipExtractor Extractor)
    {
        Extractor = new SevenZipExtractor(file);
        String[] FileNames = Extractor.ArchiveFileNames.ToArray();  
        Array.Sort(FileNames);
        return FileNames;
    }
electricalbah
  • 2,227
  • 2
  • 22
  • 36