![](/img/trans.png)
[英]How with sharpziplib get a zip file in memorystream or byte array format and unpack it
[英]SharpZipLib with MemoryStream
我一直在用asp.net开发一个Web应用程序,并且对SharZipLib有疑问。 我有一个名为Template.odt的文件(来自Open Office),该文件是压缩文件(如docx),并且其中还有一些其他文件(manified,xml,图像等)。 我需要打开此文件,更改名为content.xml和styles.xml的文件,并保存在另一个.odt文件中并提供给我的客户端。 但是我不确定是否可以使用临时文件,因此我在考虑如何使用MemoryStream进行操作。
看我得到了什么:
protected byte[ GetReport() {
Stream inputStream = File.OpenRead(Server.MapPath("~/Odt/Template.odt"));
var zipInputStream = new ZipInputStream(inputStream);
var outputStream = new MemoryStream();
var zipOutputStream = new ZipOutputStream(outputStream);
ZipEntry entry = zipInputStream.GetNextEntry();
while (entry != null) {
if (entry.Name == "content.xml")
// how change the content ?
else if (entry.Name == "styles.xml")
// how change the content ?
// how to add it or create folders in the output ?
zipOutputStream.Write( ??? );
entry = zipInputStream.GetNextEntry();
}
zipOutputStream.Flush();
return outputStream.ToArray();
}
我不确定这是否正确,但我认为这是正确的。
我尝试从ZipEntry实例获取ExtraData,但我得到它为null,这正常吗?
有人能帮我吗?
谢谢
您可以在此处找到有关如何更新内存中ZIP文件的示例: http : //wiki.sharpdevelop.net/SharpZipLib_Updating.ashx#Updating_a_zip_file_in_memory_1
在您的情况下,您可能必须将content.xml
加载到XmlDocument
或XDocument
以进行修改-但这取决于您要完全更改的内容。
附带说明:使用流时,请确保要丢弃它们。 最简单的方法是将操作包装在using
语句中:
using(var inputStream = File.OpenRead(Server.MapPath("~/Odt/Template.odt")))
{
// ...
}
有关更多信息,请访问: http : //www.codeproject.com/Articles/6564/Understanding-the-using-statement-in-C
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.