我想知道在使用.net的ZipPackage类时,有没有办法避免在zip文件中包含[Content_Types].xml文件。
编号。
Zip包不是普通的Zip文件。它们必须遵循一种强制性结构,并在根目录中包含Content_Types.xml
文件。ZipPackage = ZipArchive + Structure
。
如果您想创建(特别是如果您想读取)普通的Zip归档文件,则需要使用额外的库,BCL中没有支持。
请参见SharpZipLib(GPL)和DotNetZip
ZipPackage
,添加你的部分,然后在处理ZipPackage
之前复制流(调用.Flush()
),那么生成的zip文件仅包含你添加的部分,而没有"[Context_Types].xml"。 - Matt Kleinpublic static void AddFilesToZip(string zipFilename, List<String> filesToAdd)
{
using (var memStream = new MemoryStream())
{
using (Package zip = System.IO.Packaging.Package.Open(memStream, FileMode.Create))
{
foreach (var fileToAdd in filesToAdd)
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
//Existing parts not likely in fresh memory stream
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);
}
}
}
//The zip Package will add an XML content type file to memeory stream when it closes
//so before it closes we save the memorystream to physical zip file.
using (FileStream zipfilestream = new FileStream(zipFilename, FileMode.Create, FileAccess.Write))
{
memStream.Position = 0;
CopyStream(memStream, zipfilestream);
}
// That's it. Zip file saved to file. Things added by package after this point will be to memory stream finally disposed.
}
}
}
CopyStream
是从哪里来的?我测试了你的代码,但当我尝试提取.zip文件的内容时,显示文件已损坏。 - Mauricio Arias Olave private void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}
您还可以使用DynamicObject
将System.IO.Packaging.Package
的私有字段_contentTypeHelper
设置为null
。
当你完成打包压缩文件时,请运行此函数:
public static void Remove_Content_Types_FromZip(string zipFileName)
{
string contents;
using (ZipFile zipFile = new ZipFile(File.Open(zipFileName, FileMode.Open)))
{
/*
ZipEntry startPartEntry = zipFile.GetEntry("[Content_Types].xml");
using (StreamReader reader = new StreamReader(zipFile.GetInputStream(startPartEntry)))
{
contents = reader.ReadToEnd();
}
XElement contentTypes = XElement.Parse(contents);
XNamespace xs = contentTypes.GetDefaultNamespace();
XElement newDefExt = new XElement(xs + "Default", new XAttribute("Extension", "sab"), new XAttribute("ContentType", @"application/binary; modeler=Acis; version=18.0.2application/binary; modeler=Acis; version=18.0.2"));
contentTypes.Add(newDefExt);
contentTypes.Save("[Content_Types].xml");
zipFile.BeginUpdate();
zipFile.Add("[Content_Types].xml");
zipFile.CommitUpdate();
File.Delete("[Content_Types].xml");
*/
zipFile.BeginUpdate();
try
{
zipFile.Delete("[Content_Types].xml");
zipFile.CommitUpdate();
}
catch{}
}
}
ZipPackage
的原因是什么?如果你想将现有文档创建为 zip 文件,最好使用System.IO.Compression
命名空间中的GZipStream
和相关类。 - shahkalpesh