在Windows中由C#DeflateStream创建的打开文件

时间:2019-04-10 21:02:27

标签: c# compression

我正在调试一些数据(JSON字符串),这些数据将发送到使用DeflateStream在C#中压缩的PHP API:

using (var writer = new StreamWriter(new MemoryStream())){
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    request.Headers.Add("Content-Encoding", "gzip");
    request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
    byteArray = Compress(new MemoryStream(Encoding.UTF8.GetBytes(incomingData)));
    Random random = new Random();
    int randomNumber = random.Next(0, 100);
    File.WriteAllBytes(@"compressed-"+ randomNumber + ".gz", byteArray);
}

这是Compress()

的定义
private static byte[] Compress(Stream input)
        {
            using (var compressStream = new MemoryStream())
            using (var compressor = new DeflateStream(compressStream, CompressionMode.Compress))
            {
                input.CopyTo(compressor);
                compressor.Close();
                return compressStream.ToArray();
            }
        }

是否可以打开Windows中创建的该文件?我尝试了7zip和Winrar无济于事。如果我要使用GZipStream,则可以打开它,但是PHP与该格式不兼容。

1 个答案:

答案 0 :(得分:1)

它不写标题信息和元数据,因此您不能像zip文件一样“打开”它……您总是可以简单地执行相反操作(即使用DeflateStream进行解压缩)来获取原始数据...