This is a migrated thread and some comments may be shown as answers.

compressing and decompressing functions

0 Answers 14 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Milind
Top achievements
Rank 1
Milind asked on 29 Oct 2017, 09:52 AM

compressing and decompressing functions -

 

private string CompressString(string str, string strInflateDeflate)
        {
            string strReturnValue = "";

            if (str != "")
            {
                MemoryStream memoryStream = new MemoryStream();
                CompressionMethod method = (CompressionMethod)Enum.Parse(typeof(CompressionMethod), strInflateDeflate, false);
                CompressionSettings settings;
                switch (strInflateDeflate)
                {
                    case "Deflate":
                        {
                            settings = new DeflateSettings();
                        }
                        break;
                    case "Lzma":
                        {
                            settings = new LzmaSettings();
                        }
                        break;
                    default:
                        {
                            settings = new StoreSettings();
                        }
                        break;
                }

                using (CompressedStream compressedStream = new CompressedStream(memoryStream, StreamOperationMode.Write, settings))
                {
                    //byte[] decBytes = Encoding.UTF8.GetBytes(str);
                    byte[] decBytes = Encoding.UTF8.GetBytes(str);
                    compressedStream.Write(decBytes, 0, decBytes.Length);
                }

                strReturnValue = Convert.ToBase64String(memoryStream.ToArray());
            }

            return strReturnValue;
        }

        private string DecompressString(string str, string strInflateDeflate)
        {
            string strReturnValue = "";

            if (str != "")
            {
                MemoryStream memoryStream = new MemoryStream();
                MemoryStream outputStream = new MemoryStream();
                CompressionMethod method = (CompressionMethod)Enum.Parse(typeof(CompressionMethod), strInflateDeflate, false);
                CompressionSettings settings;
                switch (strInflateDeflate)
                {
                    case "Deflate":
                        {
                            settings = new DeflateSettings();
                        }
                        break;
                    case "Lzma":
                        {
                            settings = new LzmaSettings();
                        }
                        break;
                    default:
                        {
                            settings = new StoreSettings();
                        }
                        break;
                }

                byte[] byteArray = Encoding.ASCII.GetBytes(contents);

                memoryStream = new MemoryStream();
                memoryStream.Write(byteArray, 0, byteArray.Length);

                using (CompressedStream compressedStream = new CompressedStream(memoryStream, StreamOperationMode.Read, new DeflateSettings()))
                {
                    compressedStream.CopyTo(outputStream);
                }

                strReturnValue = Convert.ToBase64String(outputStream.ToArray());
            }

            return strReturnValue;
        }

 

 

The above functions are not giving proper output.

Can anybody suggest what corrections are required ?

 

Thanks

 

Milind Shevade

No answers yet. Maybe you can help?

Tags
ZipLibrary
Asked by
Milind
Top achievements
Rank 1
Share this question
or