GZipStream.Write 方法

发布于 2024-11-09 03:19:19 字数 501 浏览 0 评论 0原文

我已经阅读了一段时间有关 GZipStream 及其 Write 方法的内容。我试图做的是转换流中的压缩数据并将其放入字节数组中。我将在下面留下我的代码,因为我相信它会有很大帮助。

public static void Compress(byte[] fi)
{
    using (MemoryStream inFile = new MemoryStream(fi))
    using (FileStream outFile = File.Create(@"C:\Compressed.exe"))
    using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
    {
        inFile.CopyTo(Compress);
    }
}

我不想写入磁盘上的文件,而是想将压缩数据写入字节数组,然后返回字节数组(当然假设我将其设为函数)。

I have been reading for a short while about GZipStream and its Write method. What I am attempting to do is convert the compressed data from the stream and put it in a byte array. I will leave you with my code below as I believe it will help significantly.

public static void Compress(byte[] fi)
{
    using (MemoryStream inFile = new MemoryStream(fi))
    using (FileStream outFile = File.Create(@"C:\Compressed.exe"))
    using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
    {
        inFile.CopyTo(Compress);
    }
}

Rather than writing to a file on my disk, I would like to write the compressed data onto a byte array, and then return the byte array (assuming I made this a function of course).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-11-16 03:19:19

您只需使用另一个 MemoryStream 及其 ToArray 方法

public static byte[] Compress(byte[] fi)
{
    using (MemoryStream outFile = new MemoryStream())
    {
        using (MemoryStream inFile = new MemoryStream(fi))
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            inFile.CopyTo(Compress);
        }
        return outFile.ToArray();
    }
}

You can simply use use another MemoryStream and its ToArray method.

public static byte[] Compress(byte[] fi)
{
    using (MemoryStream outFile = new MemoryStream())
    {
        using (MemoryStream inFile = new MemoryStream(fi))
        using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress))
        {
            inFile.CopyTo(Compress);
        }
        return outFile.ToArray();
    }
}
花开半夏魅人心 2024-11-16 03:19:19

来自我的扩展库之一

public static string Compress(this string s)
    {
        byte[] bytesToEncode = Encoding.UTF8.GetBytes(s);
        return Convert.ToBase64String(bytesToEncode.Compress());
    }

    public static byte[] Compress(this byte[] bytesToEncode)
    {
        using (MemoryStream input = new MemoryStream(bytesToEncode))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
            {
                input.CopyTo(zip);
            }
            return output.ToArray();
        }
    }

    public static string Explode(this string s)
    {
        byte[] compressedBytes = Convert.FromBase64String(s);
        return Encoding.UTF8.GetString(compressedBytes.Explode());
    }

    public static byte[] Explode(this byte[] compressedBytes)
    {
        using (MemoryStream input = new MemoryStream(compressedBytes))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
            {
                zip.CopyTo(output);
            }
            return output.ToArray();
        }
    }

From one of my extension libraries

public static string Compress(this string s)
    {
        byte[] bytesToEncode = Encoding.UTF8.GetBytes(s);
        return Convert.ToBase64String(bytesToEncode.Compress());
    }

    public static byte[] Compress(this byte[] bytesToEncode)
    {
        using (MemoryStream input = new MemoryStream(bytesToEncode))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
            {
                input.CopyTo(zip);
            }
            return output.ToArray();
        }
    }

    public static string Explode(this string s)
    {
        byte[] compressedBytes = Convert.FromBase64String(s);
        return Encoding.UTF8.GetString(compressedBytes.Explode());
    }

    public static byte[] Explode(this byte[] compressedBytes)
    {
        using (MemoryStream input = new MemoryStream(compressedBytes))
        using (MemoryStream output = new MemoryStream())
        {
            using (System.IO.Compression.GZipStream zip = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
            {
                zip.CopyTo(output);
            }
            return output.ToArray();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文