需要动态地将一些文件打包成.zip来创建SCORM包,任何人都知道如何使用代码完成这项工作?是否可以在.zip内部动态构建文件夹结构?
Che*_*eso 22
您可以将zip直接写入Response.OutputStream.代码如下所示:
Response.Clear();
Response.BufferOutput = false; // for large files...
System.Web.HttpContext c= System.Web.HttpContext.Current;
String ReadmeText= "Hello!\n\nThis is a README..." + DateTime.Now.ToString("G");
string archiveName= String.Format("archive-{0}.zip",
DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "filename=" + archiveName);
using (ZipFile zip = new ZipFile())
{
// filesToInclude is an IEnumerable<String>, like String[] or List<String>
zip.AddFiles(filesToInclude, "files");
// Add a file from a string
zip.AddEntry("Readme.txt", "", ReadmeText);
zip.Save(Response.OutputStream);
}
// Response.End(); // no! See http://stackoverflow.com/questions/1087777
Response.Close();
Run Code Online (Sandbox Code Playgroud)
DotNetZip是免费的.
小智 15
您不必再使用外部库.System.IO.Packaging具有可用于将内容放入zip文件的类.然而,它并不简单. 这是一个带有示例的博客文章(最后是它;挖掘它).
链接不稳定,所以这里是Jon在帖子中提供的示例.
using System;
using System.IO;
using System.IO.Packaging;
namespace ZipSample
{
class Program
{
static void Main(string[] args)
{
AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe");
}
private const long BUFFER_SIZE = 4096;
private static void AddFileToZip(string zipFilename, string fileToAdd)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "",CompressionOption.Normal);
using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
CopyStream(fileStream, dest);
}
}
}
}
private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
{
long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
byte[] buffer = new byte[bufferSize];
int bytesRead = 0;
long bytesWritten = 0;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
{
outputStream.Write(buffer, 0, bytesRead);
bytesWritten += bytesRead;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
If you're using .NET Framework 4.5 or newer you can avoid third-party libraries and use the System.IO.Compression.ZipArchive native class.
Here’s a quick code sample using a MemoryStream and a couple of byte arrays representing two files:
byte[] file1 = GetFile1ByteArray();
byte[] file2 = GetFile2ByteArray();
using (MemoryStream ms = new MemoryStream())
{
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
var zipArchiveEntry = archive.CreateEntry("file1.txt", CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file1, 0, file1.Length);
zipArchiveEntry = archive.CreateEntry("file2.txt", CompressionLevel.Fastest);
using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file2, 0, file2.Length);
}
return File(ms.ToArray(), "application/zip", "Archive.zip");
}
Run Code Online (Sandbox Code Playgroud)
您可以在 MVC 控制器中使用它返回一个ActionResult: 或者,如果您需要物理创建 zip 存档,您可以将其保存MemoryStream到磁盘或完全用FileStream.
| 归档时间: |
|
| 查看次数: |
36631 次 |
| 最近记录: |