我正在开发的应用程序需要将xml文件压缩成zip文件,并通过http请求将其发送到web服务。因为我不需要保存zip文件,所以我只是在内存中执行压缩。web服务拒绝了我的请求,因为zip文件显然格式不正确。
我知道有一个解决办法
this question
它工作得很好,但它使用
StreamWriter
. 我的问题是
StreamWriter
需要编码或假定
UTF-8
,我不需要知道xml文件的编码。我只需要从这些文件中读取字节,并将它们存储在zip文件中,无论它们使用何种编码。
所以,要明确的是,这个问题与编码无关,因为我不需要将字节转换为文本或oposite。我只需要压缩
byte[]
.
我正在使用下一个代码来测试zip文件的格式有多不正确:
static void Main(string[] args)
{
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
string xmlDeclaration = "<?xml version=\"1.0\" encoding=\"" + encoding.WebName.ToUpperInvariant() + "\"?>";
string xmlBody = "<Test>ª!\"·$%/()=?¿\\|@#~â¬Â¬'¡º</Test>";
string xmlContent = xmlDeclaration + xmlBody;
byte[] bytes = encoding.GetBytes(xmlContent);
string fileName = "test.xml";
string zipPath = @"C:\Users\dgarcia\test.zip";
Test(bytes, fileName, zipPath);
}
static void Test(byte[] bytes, string fileName, string zipPath)
{
byte[] zipBytes;
using (var memoryStream = new MemoryStream())
using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, leaveOpen: false))
{
var zipEntry = zipArchive.CreateEntry(fileName);
using (Stream entryStream = zipEntry.Open())
{
entryStream.Write(bytes, 0, bytes.Length);
}
zipBytes = memoryStream.ToArray();
}
using (var fileStream = new FileStream(zipPath, FileMode.OpenOrCreate))
{
fileStream.Write(zipBytes, 0, zipBytes.Length);
}
}
如果我试图打开该文件,则会出现“意外的文件结尾”错误。很明显,web服务正确地报告了格式错误的zip文件。到目前为止,我已经尝试了:
-
冲洗
entryStream
.
-
关闭
入口流
.
-
冲洗和关闭
入口流
.
请注意,如果我打开
zipArchive
直接从
fileStream
zip文件的格式没有错误。然而
文件流
只是作为测试,我需要在内存中创建zip文件。