Question Extract file

Eric_03

Member
Joined
Jul 25, 2022
Messages
14
Location
France
Programming Experience
Beginner
Hi,

How to extract only files with PEZ character 10, 11 and 12 in file name from this code:

C#:
using SharpCompress.Common;
using SharpCompress.Readers;
using System;
using System.IO;

namespace ExcelWorkbook1
{
    class SharpCompress
    {
        public static void UnTAR(String tarFilePath, string uncompressFolderPath)
        {
            using (Stream stream = File.OpenRead(tarFilePath))
            {
                var reader = ReaderFactory.Open(stream);
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory)
                    {
                        ExtractionOptions opt = new ExtractionOptions
                        {
                            ExtractFullPath = true,
                            Overwrite = true
                        };
                        reader.WriteEntryToDirectory(uncompressFolderPath, opt);
                    }
                }
            }
        }
    }
}


extract PEZ.png


thank you

Eric
 
Solution
Since it's open source, you can look at the source code itself.


Looks like Key contains the filename. That's validated by the sample usage page (go all the way to the bottom):
Did you look at the documentation for that library you are using? There is a high probability that Entry had other properties that just IsDirectory, like the name of the entry. You can add a condition to check that property.
 
Since it's open source, you can look at the source code itself.


Looks like Key contains the filename. That's validated by the sample usage page (go all the way to the bottom):
 
Solution
C#:
using SharpCompress.Common;
using SharpCompress.Readers;
using System;
using System.IO;

namespace ExcelWorkbook1
{
    class SharpCompress
    {

        public static void UnTAR(String tarFilePath, string uncompressFolderPath)
        {
            using (Stream stream = File.OpenRead(tarFilePath))
            {
                var reader = ReaderFactory.Open(stream);
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory)
                    {
                        String str = reader.Entry.Key;
                        if (str.Substring(11, 3) == "PEZ")
                        {
                            ExtractionOptions opt = new ExtractionOptions
                            {
                                ExtractFullPath = true,
                                Overwrite = true
                            };
                            reader.WriteEntryToDirectory(uncompressFolderPath, opt);
                        }
                    }
                }
            }
        }
    }
}
 
Maybe better written as:

C#:
using SharpCompress.Common;
using SharpCompress.Readers;
using System;
using System.IO;

namespace ExcelWorkbook1
{
    class SharpCompress
    {
        public static void UnTarPezFiles(String tarFilePath, string uncompressFolderPath)
        {
            var extractOptions = new ExtractionOptions
            {
                ExtractFullPath = true,
                Overwrite = true
            };

            using (var stream = File.OpenRead(tarFilePath))
            using (var reader = ReaderFactory.Open(stream))
            {
                while (reader.MoveToNextEntry())
                {
                    if (!reader.Entry.IsDirectory &&
                        reader.Entry.Key.Substring(11, 3) == "PEZ")
                    {
                        reader.WriteEntryToDirectory(uncompressFolderPath, extractOptions);
                    }
                }
            }
        }
    }
}
 
Hi,

I'm beginner to c# and I don't understand why it's better to write the code like this. Can you tell me a little more please so that I can progress. I will of course replace my code with yours.

Thanks very much

Eric
 
  • The suggested code creates the reader with a using statement, so it will be closed implicitly.
  • The ExtractionOptions is only created once, instead of on every iteration of the loop.
  • The code is simplified by combining two if statements into one as their separation serves no useful purpose.
 
Back
Top Bottom