从文件夹中的所有文件创建zip文件 [英] Create zip file from all files in folder

查看:64
本文介绍了从文件夹中的所有文件创建zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件夹中的所有文件创建一个zip文件,但是无法在线找到任何相关的代码段.我正在尝试做这样的事情:

I'm trying to create a zip file from all files in a folder, but can't find any related snippet online. I'm trying to do something like this:

DirectoryInfo dir = new DirectoryInfo("somedir path");
ZipFile zip = new ZipFile();
zip.AddFiles(dir.getfiles());
zip.SaveTo("some other path");

非常感谢您的帮助.

我只想压缩文件夹中的文件,而不是子文件夹.

edit: I only want to zip the files from a folder, not it's subfolders.

推荐答案

在您的项目中引用System.IO.Compression和System.IO.Compression.FileSystem

Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project

using System.IO.Compression;

string startPath = @"c:\example\start";//folder to add
string zipPath = @"c:\example\result.zip";//URL for your ZIP file
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = @"c:\example\extract";//path to extract
ZipFile.ExtractToDirectory(zipPath, extractPath);

要仅使用文件,请使用:

To use files only, use:

//Creates a new, blank zip file to work with - the file will be
//finalized when the using statement completes
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
    foreach (string file in Directory.GetFiles(myPath))
    {
        newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file));
    }              
}

这篇关于从文件夹中的所有文件创建zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆