Using SevenZip to extract Zipped files within a zip file

electricalbah

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

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;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Extract file using SevenZip

Julia: Extract Zip files within a Zip file

Distinguishing between *.zip file and zipped container files

Editing a file within a zipped file using JSZip

How to extract a password protected zip file that is zipped using PKWARE SecureZip in Python? (Windows 10)

Using a batch file, can I rename files within a ZIP file?

Using Zip to read a file vertically and search through the zipped list

Unzip a file zipped in Unix using 7zip

Using R to download zipped data file, extract, and import .csv

extract zip files using python

extract files within an exe file

Extract a specific *.zip within a *.zip file to a certain directory

How to compress / decompress string with using SevenZip - 7Zip

File zipped using 7za cannot be unzipped using zip utility

Finding a file within recursive directory of zip files

Extracting byte sizes of files within a zip file

Listing the contents of zip files within a tar file

Reading files inside a subdir within a zip file

Extract only text files from a zip file

Extract ZIP file, including hidden files

Unzip a zipped file that is saved using name and date of download like this (Query Transaction History_20221126.zip)

not extract .zip files after download using PHP

How to extract .zip files to a directory within a lambda function

Extract ZIP file and then all sub-zip files

extract a zip file with a windows shortcut using PowerShell

Download zip file locally to tempfile, extract files to tempfile, and list the files

Unable to delete file after using SevenZip.openArchive method in java

Is there a command to list the compressed file sizes for files within a .zip file?

Extracting zipped files using JSZIP in javascript