简体   繁体   English

ZIP压缩目录和子目录c#中的所有文件(* .txt)

[英]ZIP compress all files (*.txt) in a directory and subdirectory c#

I need your help, I am new to C#. 我需要您的帮助,我是C#的新手。

I have a program that compress a folder with all its files and folders, but I would like to just compress a specific kind of file. 我有一个程序可以压缩包含所有文件和文件夹的文件夹,但是我只想压缩特定种类的文件。

I use this code to compress: 我使用以下代码进行压缩:

if (!File.Exists(name_of_zip_folder))
{
    ZipFile.CreateFromDirectory(folder, name_of_zip_folder);
}

I would like to do the following: 我要执行以下操作:

public void Zipfunction(string folder, List<string> files_to_compress){
    //compress these kind of files, keeping the structur of the main folder
}

For example: 例如:

Zipfunction(main_folder, new List<string> { "*.xlsx", "*.html"});

How could I compress only specific file types? 如何仅压缩特定文件类型?

This is a code snippet from Jan Welker (Originally posted here) that shows how to compress individual files using SharpZipLib 这是Jan Welker的代码片段(最初在此处发布) ,显示了如何使用SharpZipLib压缩单个文件

private static void WriteZipFile(List<string> filesToZip, string path, int compression)
{

if (compression < 0 || compression > 9)
    throw new ArgumentException("Invalid compression rate.");

if (!Directory.Exists(new FileInfo(path).Directory.ToString()))
    throw new ArgumentException("The Path does not exist.");

foreach (string c in filesToZip)
    if (!File.Exists(c))
        throw new ArgumentException(string.Format("The File{0}does not exist!", c));


Crc32 crc32 = new Crc32();
ZipOutputStream stream = new ZipOutputStream(File.Create(path));
stream.SetLevel(compression);

for (int i = 0; i < filesToZip.Count; i++)
{
    ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
    entry.DateTime = DateTime.Now;

    using (FileStream fs = File.OpenRead(filesToZip[i]))
    {
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        entry.Size = fs.Length;
        fs.Close();
        crc32.Reset();
        crc32.Update(buffer);
        entry.Crc = crc32.Value;
        stream.PutNextEntry(entry);
        stream.Write(buffer, 0, buffer.Length);
    }
}
stream.Finish();
stream.Close();

} }

Now combinding this with something like this that gets files from a specific folder and you should have what you were asking for if I did not missunderstand your request? 现在,将其与类似这样的东西(从特定文件夹中获取文件)结合起来,如果我没有错过您的请求,您应该拥有所要的内容吗?

var d = new DirectoryInfo(@"C:\temp");
FileInfo[] Files = d.GetFiles("*.txt"); //Get txt files
List<string> filesToZip = new List<string>();
foreach(FileInfo file in Files )
{
    filesToZip.add(file.Name);
}

Hope it helps //KH. 希望对// KH有帮助。

To create zip archives from a wildcard filtered source you use ZipArchive and manually create a ZipArchiveEntry for any file that meets the specified search criteria. 要从通配符过滤的源创建zip存档,请使用ZipArchive并为满足指定搜索条件的任何文件手动创建ZipArchiveEntry The last page lists a sample that illustrates this. 最后一页列出了说明这一点的示例。 To search in a directory with a wildcard pattern you can use Directory.GetFiles . 要使用通配符模式搜索目录,可以使用Directory.GetFiles

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

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