how to avoid [Content_Types].xml in .net's ZipPackage class

前端 未结 6 994
星月不相逢
星月不相逢 2021-01-07 19:09

I wish to know if there is any way to avoid to have a [Content_Types].xml file inside the zip file while using .net\'s ZipPackage

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 19:52

    Run this function when you finish packing your zip file:

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

提交回复
热议问题