0

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();

        }

    }
}
Andy
  • 12,859
  • 5
  • 41
  • 56
Lobo
  • 31
  • 4
  • It wouldn't surprise me at all if you aren't able to do this with just the System.IO.Compression classes. They aren't exactly feature-rich unfortunately. – Broots Waymb Sep 07 '21 at 19:16
  • 1
    "I have two question" please don't ask multiple questions as one. – Klaus Gütter Sep 08 '21 at 05:16
  • How do you create those zip files? If you are only interested in the filenames in the zip, the first one should contain all of them. Have you tried to read the entries with ZipArchive only from the first file? – Steeeve Sep 08 '21 at 16:45

3 Answers3

1

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);
                                }
                            }
                        }
                    }
Lobo
  • 31
  • 4
  • 1
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution. – Community Sep 08 '21 at 00:43
  • ok I need to read only the filenames (contents) of the zip files. But unfortunately I have split zip files on the CD and they look like this (xxx.zip xxx.zip.001 xxx.zip.002) and I have a problem because it does not read files with the xxx.zip.001 extension. The zip files themselves are read correctly and no matter if via DotNetzip or other solutions. There is always a problem with a split zip file. (xxx.zip.001 xxx.zip.002 etc.) – Lobo Sep 08 '21 at 06:43
1

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;
        }
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Lobo
  • 31
  • 4
0

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.

eglease
  • 2,445
  • 11
  • 18
  • 28
  • This is the original library https://github.com/DinoChiesa/DotNetZip. Looks like it has not been changed in four years. – eglease Sep 07 '21 at 19:37
  • I added it by dotnetzip and unfortunately there is the same problem; / do to read z part zip files xxxx.zip.001 xxxx.zip.002 etc. – Lobo Sep 07 '21 at 20:19
  • because I don't want to unpack them, but only read the names of the files packed in these parts – Lobo Sep 07 '21 at 20:24
  • `e` should have properties that you can read like `e.FileName`, see http://grapevine.dyndns-ip.com/download/authentec/download/dotnetzip/examples.htm – eglease Sep 07 '21 at 20:31
  • i read it to text, from xxxx.zip file but can't read names of files from xxxx.zip.001 xxxx.zip.002 etc – Lobo Sep 07 '21 at 20:39
  • Are all zip files in the same location? I will need to try to see why it is not working. – eglease Sep 07 '21 at 21:05
  • Looks like this is the issue: https://stackoverflow.com/questions/32539791/how-to-unzip-a-split-zip-archive-using-dotnetlib DotNetZip requires a special naming convention and does not work if the file was originally created with 7-zip – eglease Sep 07 '21 at 21:59
  • https://github.com/squid-box/SevenZipSharp could be another solution but it requires 7-zip dlls. – eglease Sep 07 '21 at 22:09
  • i do it with SevenZipSharp and its read only 7zip files not zip files, i get error "SevenZip.SevenZipArchiveException: „Invalid archive: open/read error! Is it encrypted and a wrong password was provided? If your archive is an exotic one, it is possible that SevenZipSharp has no signature for its format and thus decided it is TAR by mistake.” – Lobo Sep 07 '21 at 22:15
  • SevenZipSharp should support all 7-zip formats including zip. Another solution could be with `System.IO.Compression` and `CombinationStream`. See https://stackoverflow.com/questions/42803599/unzip-splitted-zip-file-c-sharp-load-parts-from-cd – eglease Sep 07 '21 at 22:29