Lobo
Lobo

Reputation: 31

How to read multi zip files in c#

I have two question, can you help me, I have a function that reads zip files beautifully but I can't read a split zip file anymore.

zip.001 zip.002

how to read zip.001 file content? and whether it is possible to read the content with special characters, e.g. ó ć ę ł, etc.

thank you in advance for your help.

regards Lobos

using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{



    openFileDialog1.InitialDirectory = d.Name;
    //openFileDialog1.InitialDirectory = @"c:\";
    openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {

        ZipArchive zip = ZipFile.OpenRead(openFileDialog1.FileName);

        listBox1.Items.Clear();
        foreach (ZipArchiveEntry entry in zip.Entries)
        {

            string unicodeString = entry.FullName;

            // Create two different encodings.
            Encoding ascii = Encoding.GetEncoding("CP852");
            Encoding unicode = Encoding.Unicode;

            // Convert the string into a byte array.
            byte[] unicodeBytes = unicode.GetBytes(unicodeString);

            // Perform the conversion from one encoding to the other.
            byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

            listBox1.Items.Add(unicodeString);
            listBox1.ForeColor = Color.FromArgb(1, 150, 227);

            //textBox5.AppendText(entry.FullName);
            // toolStripStatusLabel2.Text = " Ilość elementów:  " + listBox1.Items.Count.ToString();

        }

    }
}

Upvotes: 0

Views: 1125

Answers (3)

Lobo
Lobo

Reputation: 31

I did it in a different way, unfortunately I had to set aside "zip" as linked files and focus on the linked "rar" which reads super, so "zip" only reads as zip with DotNetZip, and the archive split into parts reads by rar, exactly by: SharpCompress.

if (ext == ".rar")
{
    //------------------------------------------------------------------------------

    var archive = ArchiveFactory.Open(item);
    foreach (var entry in archive.Entries)
    {
        if (!entry.IsDirectory)
        {
            // Console.WriteLine(entry.Key);
            listBox1.Items.Add(entry.Key);
            listBox1.ForeColor = Color.White;
        }
    }

    //------------------------------------------------------------------------------

}
else if (ext == ".zip")
{

    using (ZipFile zip = new ZipFile(item))

    {
        zip.AlternateEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        // Loop through the archive's files.

        foreach (ZipEntry zip_entry in zip)
        {
            listBox1.Items.Add(zip_entry.FileName);
            listBox1.ForeColor = Color.White;
        }
    }
}

Upvotes: 1

Lobo
Lobo

Reputation: 31

SevenZipSharp and its read only 7zip files not zip files, i get error.

using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
                    {
                        openFileDialog1.InitialDirectory = d.Name;               
                        openFileDialog1.Filter = "zip files (*.zip)|*.zip |All files (*.*)|*.*";
                        openFileDialog1.FilterIndex = 2;
                        openFileDialog1.RestoreDirectory = true;


                        if (openFileDialog1.ShowDialog() == DialogResult.OK)
                        {                    
                            SevenZip.SevenZipCompressor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\\"),"7z.dll"));
                            SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory + "X86\\"), "7z.dll"));
                            using (var zip = new SevenZipExtractor(openFileDialog1.FileName))
                            {
                                foreach (var file in zip.ArchiveFileData)
                                {
                                    listBox1.Items.Add(file.FileName);
                                    listBox1.ForeColor = Color.FromArgb(1, 150, 227);
                                }
                            }
                        }
                    }

Upvotes: 1

eglease
eglease

Reputation: 2754

You can use DotNetZip as long as your zip file was not created with 7-zip and follows the file names filename.z01, filenamez02, ...

  using (ZipFile zip = ZipFile.Read(NameOfExistingZipFile))
  {
    foreach (ZipEntry e in zip)
    {
      Console.WriteLine($"{e.FileName} {e.LastModified}");
    }
  }

Another possibility is SevenZipSharp but it requires 7-zip .DLLs to be installed as well.

Upvotes: 0

Related Questions