一佳一

记录像1+1一样简洁的代码

导航

zip压缩解压

Posted on 2013-05-21 17:19  一佳一  阅读(1404)  评论(0编辑  收藏  举报

网上有很多zip操作的,但是存在不少问题,只能自己改了一个能用的,希望能帮助需要的朋友,如果觉得好用记得给我留言,加下人气哦,废话不多多说,上代码\

 /**/
        /// <summary>
        /// 实现压缩功能
        /// </summary>
        /// <param name="filenameToZip">要压缩文件(绝对文件路径)</param>
        /// <param name="Zipedfiledname">压缩(绝对文件路径)</param>
        /// <param name="CompressionLevel">压缩比</param>
        /// <param name="password">加密密码</param>
        /// <param name="comment">压缩文件描述</param>
        /// <returns>异常信息</returns>
        public static string MakeZipFile(string[] filenameToZip, string Zipedfiledname, int CompressionLevel,
            string password, string comment)
        {
            try
            {
                //使用正则表达式-判断压缩文件路径
                System.Text.RegularExpressions.Regex newRegex = new System.Text.
                    RegularExpressions.Regex(@"^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w   ]*.*))");

                if (!newRegex.Match(Zipedfiledname).Success)
                {
                    File.Delete(Zipedfiledname);
                    return "压缩文件的路径有误!";
                }

                //创建ZipFileOutPutStream
                ZipOutputStream newzipstream = new ZipOutputStream(File.Open(Zipedfiledname,
                    FileMode.OpenOrCreate));

                //判断Password
                if (password != null && password.Length > 0)
                {
                    newzipstream.Password = password;
                }

                if (comment != null && comment.Length > 0)
                {
                    newzipstream.SetComment(comment);
                }

                //设置CompressionLevel
                newzipstream.SetLevel(CompressionLevel); //-查看0 - means store only to 9 - means best compression

                //执行压缩
                foreach (string filename in filenameToZip)
                {
                    FileStream newstream = File.OpenRead(filename);//打开预压缩文件

                    //判断路径
                    if (!newRegex.Match(Zipedfiledname).Success)
                    {
                        File.Delete(Zipedfiledname);
                        return "压缩文件目标路径不存在!";
                    }

                    byte[] setbuffer = new byte[newstream.Length];
                    newstream.Read(setbuffer, 0, setbuffer.Length);//读入文件

                    //新建ZipEntrity
                    ZipEntry newEntry = new ZipEntry(filename);

                    //设置时间-长度
                    newEntry.DateTime = DateTime.Now;
                    newEntry.Size = newstream.Length;

                    newstream.Close();

                    newzipstream.PutNextEntry(newEntry);//压入

                    newzipstream.Write(setbuffer, 0, setbuffer.Length);

                }
                //重复压入操作
                newzipstream.Finish();
                newzipstream.Close();

            }
            catch (Exception e)
            {
                //出现异常
                File.Delete(Zipedfiledname);
                return e.Message.ToString();
            }

            return "";
        }

        public static void UnZip(string zipfilepath, string unzippath)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath));

            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                //string directoryName = Path.GetDirectoryName(unzippath);
                string directoryName = unzippath;
                string fileName = Path.GetFileName(theEntry.Name);

                //生成解压目录
                Directory.CreateDirectory(directoryName);

                if (fileName != String.Empty)
                {
                    //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
                    if (theEntry.CompressedSize == 0)
                        break;
                    //解压文件到指定的目录
                    //directoryName = Path.GetDirectoryName(unzippath + theEntry.Name);
                    //建立下面的目录和子目录
                    Directory.CreateDirectory(directoryName);

                    FileStream streamWriter = File.Create(unzippath + theEntry.Name);

                    int size = 2048;
                    byte[] data = new byte[2048];
                    while (true)
                    {
                        size = s.Read(data, 0, data.Length);
                        if (size > 0)
                        {
                            streamWriter.Write(data, 0, size);
                        }
                        else
                        {
                            break;
                        }
                    }
                    streamWriter.Close();
                }
            }
            s.Close();
        }

需要加项目加上两个dll

 https://files.cnblogs.com/linyijia/zip_dll.rar