使用SharpZipLib压缩文件或文本为base64字符串

        开源框架 SharpZipLib 可以压缩与解压缩文件或文件夹为Zip格式的文件。事实上,它还可以压缩文件或文本到指定格式的字符串,从而通过网络高效传输数据。基本思路为:压缩后输出到内存流MemoryStream,然后转成字节数组,最后转换成字符串。解压缩时的顺序则相反。本文探讨压缩为Base64格式的字符串,见如下代码:

    public class TZipHelper
    {
        /// <summary>
        /// 压缩单个文件为一个Base64字符串。
        /// </summary>
        public static string ZipFileToBase64String(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
            using (MemoryStream ms = new MemoryStream())
            using (ZipOutputStream zip = new ZipOutputStream(ms))
            {
                ZipEntry entry = new ZipEntry(Path.GetFileName(fileName));
                zip.PutNextEntry(entry);

                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fs.Read(buffer, 0, buffer.Length)) > 0)
                {
                    zip.Write(buffer, 0, len);
                }

                zip.CloseEntry();  // 必须,否则报不可预料的压缩文件末端
                zip.Finish();      // 必须,否则报这个压缩文件格式未知或者数据已经被损坏

                return Convert.ToBase64String(ms.ToArray());
            }
        }

        /// <summary>
        /// 解压缩Base64字符串到文件(原文件名或指定文件名,单个文件)。
        /// </summary>
        public static void UnzipFileFromBase64String(string base64String, string toDirectoryOrFileName)
        {
            byte[] b = Convert.FromBase64String(base64String);
            using (MemoryStream ms = new MemoryStream(b))
            using (ZipInputStream zip = new ZipInputStream(ms))
            {
                ZipEntry entry = zip.GetNextEntry();
                string fileName = GetFileName(entry.Name, toDirectoryOrFileName);

                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                {
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = zip.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        fs.Write(buffer, 0, len);
                    }
                    fs.Flush();
                }
            }
        }

        /// <summary>
        /// 压缩字符串为Base64字符串。
        /// </summary>
        public static string ZipTextToBase64String(string text)
        {

            byte[] b = Encoding.Unicode.GetBytes(text);
            using (MemoryStream to = new MemoryStream())
            using (ZipOutputStream zip = new ZipOutputStream(to))
            {
                ZipEntry entry = new ZipEntry("ToBase64String");
                zip.PutNextEntry(entry);

                zip.Write(b, 0, b.Length);
                zip.CloseEntry();  // 必须,否则报不可预料的压缩文件末端
                zip.Finish();      // 必须,否则报这个压缩文件格式未知或者数据已经被损坏

                return Convert.ToBase64String(to.ToArray());
            }
        }

        /// <summary>
        /// 解压缩Base64字符串到原字符串。
        /// </summary>
        public static string UnzipTextFromBase64String(string base64String)
        {
            byte[] b = Convert.FromBase64String(base64String);
            using (MemoryStream from = new MemoryStream(b))
            using (ZipInputStream zip = new ZipInputStream(from))
            using (MemoryStream to = new MemoryStream())
            {
                ZipEntry entry = zip.GetNextEntry();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = zip.Read(buffer, 0, buffer.Length)) > 0)
                {
                    to.Write(buffer, 0, len);
                }
                b = to.ToArray();
                return Encoding.Unicode.GetString(b);
            }
        }

        private static string GetFileName(string entryName, string toDirectoryOrFileName)
        {
            if (toDirectoryOrFileName.EndsWith(@"\") == true)  // 到文件夹
            {
                return toDirectoryOrFileName + entryName;
            }
            else  // 直接到指定文件名
            {
                return toDirectoryOrFileName;
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/hulihui/article/details/81105990