我正在为 aspx 页面使用 C# 代码。我需要将多个文件转换为 Windows 默认 zip 软件可以读取的单个 zip 文件。
任何人都可以对此有所了解...?
请参阅本教程:在 .NET 中创建 Zip 存档(没有像 SharpZipLib 这样的外部库)
ZipFile zip= new ZipFile("MyNewZip.zip");
zip.AddDirectory("My Pictures", true); // AddDirectory recurses subdirectories
zip.Save();
或者您可以使用SharpZipLib。
DotNetZip是一个很好的开源软件,没有任何许可问题。
//使用这个库SharpZipLib。
使用它,您可以发送多个文件用于压缩哪个用户选择,并可以将其保存到您在客户端上指定的物理路径。
public string zipfile(string[] files)
{
string[] filenames = new string[files.Length];
for (int i = 0; i < files.Length; i++)
filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Remove(0, 10// set it according to your filename).ToString();
else
for (int i = 0; i < files.Length; i++)
filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Replace(HttpContext.Current.Request.UrlReferrer.ToString(), "");
string DirectoryName = filenames[0].Remove(filenames[0].LastIndexOf('/'));
DirectoryName = DirectoryName.Substring(DirectoryName.LastIndexOf('/') + 1).Replace("\\", "");
try
{
string newFile = HttpContext.Current.Request.PhysicalApplicationPath + "the physical path where you want to save it" + DirectoryName + ".zip";
if (File.Exists(newFile))
File.Delete(newFile);
using (ZipFile zip = new ZipFile())
{
foreach (string file in filenames)
{
string newfileName = file.Replace("\\'", "'");
zip.CompressionLevel = 0;
zip.AddFile(newfileName, "");
}
zip.Save(newFile);
}
}