-3

I have .zip archive which contains only one file - a text file. Within my WinForm I want to open the .zip file, cick on the existing text file. The text file should be displayed ListView, this is my code for now:

private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "archieve files (*.zip)|*.zip";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string filename = openFileDialog1.FileName;
            textBox1.Text = filename;
        }
        //
    }
jAC
  • 5,195
  • 6
  • 40
  • 55
komra23
  • 107
  • 8
  • http://stackoverflow.com/questions/3238173/how-to-extract-a-rar-file-in-c, http://stackoverflow.com/questions/1025863/read-content-of-rar-files-using-c-sharp – CodeCaster Oct 11 '16 at 08:21
  • @CodeCaster O_O – komra23 Oct 11 '16 at 08:21
  • You need to extract the text file temporarily to read its content. Either use WinRaR to do that or a ZIP/RAR library for .NET. If you just need the name of the file, you need a library that can enumerate the contents of the archive. – jAC Oct 11 '16 at 08:23
  • @May_be what's that supposed to mean? Please read [ask] and share your research. If you want to get a list of files in an archive, you need an API that can deal with archives, and ... read its file list. That's pretty extensively documented on the web and on this site. Also, if you want to extract a file from an archive, the same applies. It's all been asked before, try searching before asking. – CodeCaster Oct 11 '16 at 08:23
  • @JanesAbouChleih but how to write commands to the same, what is doin WinRaR ? – komra23 Oct 11 '16 at 08:23
  • @CodeCaster i have button and listview on the form. listview has columns: name, size and date modified. when i click the button i choose the winrar archieve. when i click OK the files, which are in this winrar which i chosen, must be visible in listview – komra23 Oct 11 '16 at 08:26
  • @May_be that's fine, so what have you tried to do that? See also [How to list the contents of a .zip folder in c#?](http://stackoverflow.com/questions/307774/how-to-list-the-contents-of-a-zip-folder-in-c). – CodeCaster Oct 11 '16 at 08:26
  • @May_be I think we all understand what you want to do. What you don't seem to understand is that you can't simply use other programs and access their information (data). To achieve what you want, you need a zip library. – jAC Oct 11 '16 at 08:27

1 Answers1

1

As already mentioned in the comments you can use a library for that.

.NET already has one for handling compressed zip archives called System.IO.Compression.ZipFile integrated into the .NET framework. See the MSDN.

Use this to open the .zip file (read-only) via ZipFile.OpenRead and ZipFile.Entries property to get a list of file information within the archive.

Entries is a collection of ZipFile.ZipArchiveEntry which contains a few public properties you can access. Namely the ones we need are:

  • FullName
  • LastWriteTime
  • Length

Full sample code:

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
     foreach (ZipArchiveEntry entry in archive.Entries)
     {
             if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
             {
                  ListViewItem txtItem = new ListViewItem(entry.FullName);
                  txtItem .SubItems.Add(entry.LastWriteTime);
                  txtItem .SubItems.Add(entry.Length); //Uncompressed size
                  listView.Items.Add(txtItem);
             }
     }
 } 

Put this into your Button_Click event and you're good to go.

jAC
  • 5,195
  • 6
  • 40
  • 55