Hy Guys, so after you help me to properly convert the “Compress” function ,i tryed to convert the Decompress function too
now im here
(decompress function by me)
let DecompressString (detext : string) =
let buffer = Convert.FromBase64String detext
using (new MemoryStream ()) <| fun memoryStream ->
using (new GZipStream(memoryStream, CompressionMode.Decompress, true)) <| fun gzipStream ->
memoryStream.Position <- 0L
memoryStream.Read(buffer, 0, Array.length buffer)
Encoding.UTF8.GetString(buffer)
the original Compressfunction is
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)
memoryStream.Read (compressedData, 0, compressedData.Length)
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
if i compress a string like this
File.WriteAllText("test",compressString("bla"))
(I save the file named “test” in this file is the compressed string from “bla”)
“AwAAAB+LCAAAAAAABADtvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee++997o7nU4n99//P1xmZAFs9s5K2smeIYCqyB8/fnwfPyImZfb/ADmz70cDAAAA” < seems for me bigger then before? 😀
ok now i try to decompress it again like:
File.WriteAllText("test2",DecompressString(File.ReadAllText("test")))
but in the new “test2” file is only unreadable text like:
“?"&e�ᅮ
so i think my dcompress function dont work properly.
Maybe your my heroes can help me out a second and last time ? 🙂
Anyway thanks in advance
Firstly, the memory stream you’re reading from is empty, so nothing will be written to the destination buffer.
Secondly, you’re re-using
bufferand this contains the bytes of the base 64 string argument.You can’t get the Length of a
GZipStreamso you’ll have to copy it to another memory stream: