简体   繁体   English

gZipBuffer压缩从C#转换为F#

[英]gZipBuffer Compression Convert from C# to F#

I try to learn F# and at this time i try to convert a simple C# Function to F# in some points i stuck at example var compressedData = new byte[memoryStream.Length]; 我尝试学习F#,这时我尝试在某些方面将简单的C#函数转换为F#,例如在var compressedData = new byte[memoryStream.Length]; < C# here the full c# function <C#这里是完整的c#功能

public static string CompressString(string text)
{
    byte[] buffer = Encoding.UTF8.GetBytes(text);
    var memoryStream = new MemoryStream();
    using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
    {
        gZipStream.Write(buffer, 0, buffer.Length);
    }

    memoryStream.Position = 0;

    var compressedData = new byte[memoryStream.Length];
    memoryStream.Read(compressedData, 0, compressedData.Length);

    var gZipBuffer = new byte[compressedData.Length + 4];
    Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
    Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
    return Convert.ToBase64String(gZipBuffer);
}

and here Mine half Translated version 这里是我的一半翻译版本

let compress(text:string)=
    let buffer = Encoding.UTF8.GetBytes(text)
    use memoryStream = new MemoryStream()
    let gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true)
    gZipStream.Write(buffer, 0, buffer.Length)
    memoryStream.Position <- Convert.ToInt64(0)
    let compressedData = Array.init memoryStream.Length(fun i -> byte) //< Here i stuck
    memoryStream.Read(compressedData, 0, compressedData.Length)
    use gZipBuffer = (compressedData.Length + 4)

the last three days i have searched on google to solve my problem, but i didnt find any solution. 最近三天,我已经在Google上搜索以解决我的问题,但是我没有找到任何解决方案。 I hope someone here can help me :) Thank you very much in advance 我希望这里有人可以帮助我:)提前谢谢

EDIT : Fixed the translation so it compiles (and it should work now). 编辑:修复了翻译,使其可以编译(它现在应该可以工作)。

open System
open System.IO
open System.IO.Compression
open System.Text

let compressString (text : string) =
    let buffer = Encoding.UTF8.GetBytes text
    using (new MemoryStream ()) <| fun memoryStream ->
        using (new GZipStream(memoryStream, CompressionMode.Compress, true)) <| fun gzipStream ->
            gzipStream.Write (buffer, 0, Array.length buffer)

        memoryStream.Position <- 0L

        let compressedData = Array.zeroCreate (int memoryStream.Length)

        let numBytesRead = memoryStream.Read (compressedData, 0, compressedData.Length)
        if numBytesRead = compressedData.Length then
            let gzipBuffer = Array.zeroCreate (compressedData.Length + 4)
            Buffer.BlockCopy (compressedData, 0, gzipBuffer, 4, compressedData.Length)
            Buffer.BlockCopy (BitConverter.GetBytes buffer.Length, 0, gzipBuffer, 0, 4)
            Convert.ToBase64String gzipBuffer
        else
            failwithf "Tried to read %i bytes but was only able to read %i."
                compressedData.Length numBytesRead

您正在寻找Array.zeroCreate

let compressedData = Array.zeroCreate (int memoryStream.Length)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM