如何避免.net的ZipPackage类中的[Content_Types] .xml

时间:2010-09-20 05:55:18

标签: .net .net-4.0 zip

我想知道在使用.net的 ZipPackage <时,是否有办法避免在zip文件中包含 [Content_Types] .xml 文件/ strong> class。

6 个答案:

答案 0 :(得分:9)

没有

Zip包不是(普通)Zip文件。它们必须遵循强制结构并在根中包含Content_Types.xml文件。 ZipPackage = ZipArchive + Structure

如果你想创建(特别是如果你想阅读)普通的Zip档案,你将需要一个额外的库,在BCL中没有支持。

请参阅SharpZipLib(GPL)和DotNetZip

答案 1 :(得分:4)

如果你不调用.Flush()方法,就没有这样的文件

答案 2 :(得分:2)

是的,您可以创建zip包,而无需添加额外的XML内容文件

受此链接的启发:Using System.IO.Packaging to generate a ZIP file

使用Yiping提到的上述发现,您可以避免将额外的xml文件添加到包中。在zip存档关闭之前,将zip流从内存流保存到物理zip文件:

public 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.
        }
    }
}

答案 3 :(得分:1)

伙计,这对我帮助很大!你摇滚!我不得不使用这个因为我的旧框架(3.5)。仅补充,请参见下面的CopyStream函数的实现:

    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);
    }

答案 4 :(得分:0)

您还可以将_contentTypeHelper的私人字段System.IO.Packaging.Package设置为null(使用DynamicObject)。

答案 5 :(得分:0)

在打包压缩文件后运行此功能:

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{}
        }
    }