如何压缩选定的文件?不是所有在c#中的目录中的文件 [英] How to zip selected files? not all the files in a directory in c#

查看:178
本文介绍了如何压缩选定的文件?不是所有在c#中的目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想压缩目录中的一些文件。我已经google了很多,但我总是看到关于通过下面的代码压缩一个目录(目录中的所有文件)的例子:

 字符串startPath = @subPath; 

string zipPath = @C:\result.zip;

ZipFile.CreateFromDirectory(startPath,zipPath);

我正在引用 System.IO.Compression.Filesystem.dll

这段代码压缩了startPath引用的目录中的所有文件。



我试了ZipArchive类压缩,但它压缩文件路径上的目录。例如,
假设我想压缩位于C:\directories\Graphics\a.txt的a.txt。该zip文件包含目录目录和图形,并在这些目录中的文件a.txt。这不是一个好的解决方案,因为我正在处理大尺寸数据,所以要删除文件然后再删除目录并重新粘贴。



如何压缩目录中的部分文件?

如何压缩某些文件位于不同的目录?



我正在查找这个主题2天。在.Net中是一种约束吗?

解决方案

您可以使用ZipArchive类,先创建它,然后创建一次您只需将ZipArchiveEntry对象添加到存档中即可。每个ZipArchiveEntry对象表示一个文件,您将添加到存档。



一些提示:
您可以找到更多信息和示例可以在MSDN上完成,在这里: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchive(v = vs.110).aspx 和这里: http://msdn.microsoft.com/pl-pl/library /system.io.compression.ziparchiveentry(v=vs.110).aspx

I want to zip some files in a directory. I have googled a lot but i always saw examples about zipping a directory (all files in directory) by the code below:

string startPath = @subPath;

string zipPath = @"C:\result.zip";

ZipFile.CreateFromDirectory(startPath, zipPath);

I am referencing System.IO.Compression.Filesystem.dll

This code zips all the files in the directory referenced by startPath.

I tried ZipArchive class for compressing but it compress file with directories on its path. For example, assume i want to zip "a.txt" located at "C:\directories\Graphics\a.txt". The zip file contains directories "directories" and "Graphics" and into these directories the file "a.txt". It is not a good solution to cut files then erase the directories and paste again since i am working on big size data.

How can I zip only some of files at a directory?

How can I zip some files that are located in different directories?

I am looking up the subject for 2 days. Is it a kind of constraint in .Net?

解决方案

You can use the ZipArchive class, first create it and then - once created you just add ZipArchiveEntry objects into the archive. Each of the ZipArchiveEntry objects represents a single file that you will add to the archive.

Some tips: You can find more info and a sample of how this could be done on MSDN, here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchive(v=vs.110).aspx and here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchiveentry(v=vs.110).aspx.

这篇关于如何压缩选定的文件?不是所有在c#中的目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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