2012-09-21 33 views
2

我努力學習F#和在這個時候我嘗試一個簡單的C#功能,以F# 轉換在某些方面我被困在例如var compressedData = new byte[memoryStream.Length]; < C# 這裏的完整的C#功能gZipBuffer壓縮從C#轉換爲F#

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); 
} 

這裏煤礦一半譯本

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) 

最後三天我已經搜索在谷歌解決我的問題,但我沒有找到任何解決方案。 我希望這裏有人能幫助我:) 非常感謝您提前

回答

1

您正在尋找Array.zeroCreate

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

海蘭感謝您的幫助,但是這也不要讓工作= compressedData Array.zeroCreate memoryStream.Length // <這裏新的錯誤**這種表達應該有以下類型:int 不過,他的類型:int64 – galeda

+0

使用'int'函數。我更新了我的示例來演示。 – Daniel

+0

是的你現在沒有錯誤,非常感謝你的幫助:),如果我可以我也會幫助在stackoverflow,現在嘗試轉換「解壓縮」功能,但這應該現在不是一個大問題; ) – galeda

3

編輯:固定的翻譯,以便它編譯(現在它應該工作)。

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 
+0

對不起,我還沒有看到你的帖子:)但這個作品也不是我不知道爲什麼?! trows同樣的錯誤「這個表達式應該有以下類型:int然而,他是這樣的類型:int64」 – galeda

+0

如果我正確的我只需要改變「CompressionMode.Compress」到CompressionMode.Decompress「,解壓縮字符串或? – galeda

+0

我修復了代碼示例以便編譯。我不知道如何將'CompressionMode.Compress'更改爲'CompressionMode.Decompress' - 您必須自己嘗試。 –