简体   繁体   English

如何使用7z压缩器指定字符串searchPattern?

[英]How can i specify string searchPattern using 7z compressor?

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. 我试图添加“* .txt”,因此它只压缩文本文件,但压缩许多其他格式。

When im doing: compressor.CompressDirectory(source, t, The message say: string searchPattern 当我做:compress.CompressDirectory(source,t,消息说: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. 搜索模式“* .txt”不起作用,而是仅压缩文本文件,压缩任何文件扩展名。

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' 您的代码未提供搜索模式,它设置的密码为“* .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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM