c# - zip and unzip string with Deflate -
I need to zip string and unzip
Here's the code:
Public Static Byte [] Zstrator (String Straight) using {DeflateStream gzip = New DeflateStream (Output, Compression Mode.Compay) using and
public static string UnZipStr (byte [] input) {(MemoryStream inputstream = New memorystream (input)) (DeflateStream Gzip = new DeflateStream (Input Stream, Compression Mode. DcpPress)) (reader.ReadToEnd ();) using the StreamerMinder Reader = New Streamminder (GPIP); Return System. Text. Encoding.utf 8.greatstring (inputstream.toArray ()); }} It seems that the UnZipStr method has an error. Can anyone help me?
There are two different problems first, ZipStr in MemoryStream < Before reading from / code, flush or close StreamWriter is to close DeflateStream .
In the second, UnZipStr , you are creating your result string with compressed bytes in the input stream . You should return the results of reader.ReadToEnd () instead.
This would also be a good idea to specify string encoding in StreamWriter and StreamReader constructors.
Try the following code instead:
public static byte [] ZipStr (string str) {(memorystream output = new memoryStream () ({DeflateStream gzip = new DeflateStream (Output, CompressionMode.Compress)) (StreamWriter author = new streamer (gzip, System.Text.Encoding.UTF8)) {writer.Write (str);}} Return output. Toor ();}} (Fixed Using Streamer gzip = new DeflateStream ({StreamReader Reader = New StreamReader (gzip)}} {static_reader, System.Text.Encoding.UTF8}) {Reader Reader. Read-end (); }}}}
Comments
Post a Comment