1
我的目标是使用gzip压缩文件,然后将压缩字节写入Xml部分,这意味着我需要我的代码中的压缩字节数组。我发现GZip的所有例子都只是将字节直接写入文件。阅读GZip压缩流中的数据
因此,这里是我的代码:
public ContainerFile(string[] inputFiles, string Output)
{
XmlDocument doc = new XmlDocument();
XmlNode root;
FileInfo fi;
FileStream fstream;
BinaryReader reader;
GZipStream gstream;
root = doc.CreateElement("compressedFile");
doc.AppendChild(root);
foreach (string f in inputFiles)
{
fstream = File.OpenRead(f);
MemoryStream s = new MemoryStream();
byte[] buffer = new byte[fstream.Length];
// Read the file to ensure it is readable.
int count = fstream.Read(buffer, 0, buffer.Length);
if (count != buffer.Length)
{
fstream.Close();
//Console.WriteLine("Test Failed: Unable to read data fromfile");
return;
}
fstream.Close();
gstream = new GZipStream(s, CompressionMode.Compress, true);
gstream.Write(buffer, 0, buffer.Length);
gstream.Flush();
byte[] bytes = new byte[s.Length];
s.Read(bytes, 0, bytes.Length);
File.WriteAllBytes(@"c:\compressed.gz", bytes);
}
用于调试的原因,我只是想它被装载之后将数据写入到文件中。
因此,输入文件的长度是〜4k字节。当消除器显示我时,“字节”数组的长度为〜2k。所以它看起来像压缩字节数组的大小是正确的,但它的所有值是0.
可以s.o.帮我?