Get all information from compressed files (C#)

997 views Asked by At

im currently working on a simple application where in it browse for a directory, then all the files included in that directory will be listed. Im using Path.GetFilename and other stuffs to get every file a description. And now im wodering, what about the compressed files? i want to peek inside the compressed file (zip,rar) and get all the file information i can get. how am i going to do that?

here's my code so far:

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            button1.Enabled = true;
            this.dataGridView1.Rows.Clear();
            dPaths = "";
            string[] filePaths = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);
            for (int y = 0; y < filePaths.Length; y++)
            {
                FileInfo fInfo = new FileInfo(filePaths[y]);


                if (Path.GetExtension(filePaths[y]) != ".zip" && Path.GetExtension(filePaths[y]) != ".rar")
                {
                    this.dataGridView1.Rows.Add(
                        Path.GetFileName(filePaths[y]),
                        Path.GetExtension(filePaths[y]),
                        fInfo.Length,
                        fInfo.LastWriteTime,
                        Path.GetDirectoryName(filePaths[y]));

                    dPaths = dPaths
                        + Path.GetFileName(filePaths[y]) + "?"
                        + Path.GetExtension(filePaths[y]) + "?"
                        + fInfo.Length + "?"
                        + fInfo.LastWriteTime + "?"
                        + Path.GetDirectoryName(filePaths[y])
                        + ";";
                    // Legend:
                    // ? = explode for files, in order: Filename FileExtension Filesize FileDateModified Filepath
                    // ; = explode for every files
                }
                else //zip/rar detected, everything inside is not working though
                {
                    string[] compressedfile = Directory.GetFiles(filePaths[y], "*.*", SearchOption.AllDirectories);
                    for (int x = 0; x < compressedfile.Length; x++)
                    {
                        FileInfo fInfo2 = new FileInfo(compressedfile[x]);
                        this.dataGridView1.Rows.Add(
                        Path.GetFileName(compressedfile[x]),
                        Path.GetExtension(compressedfile[x]),
                        fInfo2.Length,
                        fInfo2.LastWriteTime,
                        Path.GetDirectoryName(compressedfile[x]));

                        dPaths = dPaths
                            + Path.GetFileName(compressedfile[x]) + "?"
                            + Path.GetExtension(compressedfile[x]) + "?"
                            + fInfo2.Length + "?"
                            + fInfo2.LastWriteTime + "?"
                            + Path.GetDirectoryName(compressedfile[x])
                            + ";";
                    }
                }
            }
        }
1

There are 1 answers

0
Snorre On BEST ANSWER

For Zip files you can use .net directly. Have a look at this excellent article: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx for Rar files you would have to have a third party library. There is one at codeplex you should have a look at

http://sharpcompress.codeplex.com/