![](/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.