0

This is the methos im using for compress files:

private void Compressions(string zipFile,string sources)
        {
            try
            {
                string zipFileName = zipFile;
                string source = sources;
                string output = @"c:\temp";
                string programFilesX86 = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) + "\\Diagnostic Tool\\7z.dll";
                if (File.Exists(programFilesX86))
                {
                    SevenZipExtractor.SetLibraryPath(programFilesX86);
                }
                else
                {
                    string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\7z.dll";
                    SevenZipExtractor.SetLibraryPath(path);
                }
                string programFiles = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + "\\Diagnostic Tool\\7z.dll";
                if (File.Exists(programFiles))
                {
                    SevenZipExtractor.SetLibraryPath(programFiles);
                }
                else
                {
                    if (File.Exists(programFilesX86))
                    {
                        SevenZipExtractor.SetLibraryPath(programFilesX86);
                    }
                    else
                    {
                        string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\7z.dll";
                        SevenZipExtractor.SetLibraryPath(path);
                    }
                }
                SevenZipCompressor compressor = new SevenZipCompressor();
                compressor.ArchiveFormat = OutArchiveFormat.Zip;
                compressor.CompressionMode = CompressionMode.Create;
                compressor.TempFolderPath = System.IO.Path.GetTempPath();
                string t = Path.Combine(output, zipFileName);
                compressor.CompressDirectory(source, t,"*.txt");
                this.explorerWindow = Process.Start("explorer", String.Format("/select,{0}", t));
                this.TopMost = true;
            }
            catch (Exception err)
            {
                Logger.Write("Zip file error: " + err.ToString());
            }
        }

This is the line that compress:

compressor.CompressDirectory(source, t,"*.txt");

I tried to add "*.txt" so it will compress only text files but its compressing many other formats.

When im doing: compressor.CompressDirectory(source, t, The message say: string searchPattern

I want to compress only text files.

Edit** The problem is that its compressing any type of files and not only text files ! The search pattern "*.txt" is not working instead compressing only text files its compressing any files extentions.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
DanielVest
  • 823
  • 4
  • 20
  • 39

1 Answers1

2

Please check the method's signature and ensure you are calling the correct overload.

The three string parameter overload is defined as:

public void CompressDirectory(
        string directory, string archiveName, 
        string password)

Your code isn't providing a search pattern, it's setting a password of '*.txt'

Use one of the overloads that accepts a search pattern, eg:

public void CompressDirectory(
        string directory, string archiveName,
        string searchPattern, bool recursion)

or

public void CompressDirectory(
        string directory, string archiveName,
        string password = "", string searchPattern = "*", bool recursion = true)
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Panagiotis i tried your example also according to the sevenzip document i did: compressor.CompressDirectory(source, t,"","*.txt",false); i also tried "*.text" and only ".txt" all cases im getting exception: Index was outside the bounds of the array if im doing only : compressor.CompressDirectory(source, t); its working but once i make the password "" then searchpattern "*.txt" and false im getting the exception. what can be the reason ? – DanielVest Aug 07 '13 at 08:44
  • tried to do it with the document site of the sevenzip: https://github.com/phillipp/SevenZipSharp/blob/master/SevenZip/SevenZipCompressor.cs#L1225 line: 1225 – DanielVest Aug 07 '13 at 08:45
  • Please edit your post with a clear description of the problem. The actual line of code, and the COMPLETE exception message, including the stack trace. This will tell you where the error occurs. Besides, '.txt' is not a valid text pattern. Try '*.txt' – Panagiotis Kanavos Aug 07 '13 at 08:50