【问题标题】:How to zip files from folder but keep it separated如何从文件夹中压缩文件但保持分开
【发布时间】:2019-05-28 08:17:06
【问题描述】:

我有 1 个文件夹,其中包含许多 txt 文件。我想压缩它们但分开了。

示例:
在这个文件夹中,我有 A.txt、B.txt、C.txt。

我想压缩所有文件,但要分开,所以结果将是 A.zip、B.zip、C.zip。

string outputPath = "C:\\Users\\Desktop\\VA";
string path = outputPath + "\\VA_" + tglskrg;
foreach (string dirFile in Directory.GetDirectories(path))
{
    foreach (string fileName in Directory.GetFiles(dirFile))
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.UseUnicodeAsNecessary = true;
            zip.AddFile(); //dont know what to put
            zip.Save(); //dont know what to put
        }
    }
}

任何帮助将不胜感激。

我正在使用 dotnetzip (Ionic.zip) 和 C# Visual Studio Express 2010。

【问题讨论】:

    标签: c# dotnetzip


    【解决方案1】:

    你可以这样做:

    foreach (string fileName in Directory.GetFiles(dirFile))
    {
        var zipFile = Path.Combine(outputPath, Path.ChangeExtension(fileName, ".zip"));
        using (ZipFile zip = new ZipFile())
        {
            zip.AddFile(fileName); // add A.txt to the zip file
            zip.Save(zipFile); // save as A.zip
        }
    }
    

    这会获取文件夹dirFile 中的所有文件,并将它们保存在outputPath 下,文件名相同,但扩展名替换为.zip。

    【讨论】:

    • Welp,我几乎完全一样,但你快了 20 秒。运气不好,我会删除我的。
    • 抱歉回复晚了,刚到办公室,非常感谢,它真的有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多