ICSharpCode.SharpZipLib .dll 使用说明

 public class FileUtil
    {
        /// <summary>压缩文件,打包
        ///
        /// </summary>
        /// <param name="strTargetFilePath">压缩包存放地完整路径</param>
        /// <param name="strLocalPath">源路径</param>
        public static bool Zip(string strTargetFilePath, string strLocalPath)
        {
            bool isZipSucess = false;
            string strFileName = string.Empty;
            Crc32 crc = new Crc32();
            try
            {
                //zip压缩打包
                using (ZipOutputStream zip = new ZipOutputStream(System.IO.File.Create(strTargetFilePath)))
                {
                    System.IO.DirectoryInfo zipdir = new System.IO.DirectoryInfo(strLocalPath);
                    System.IO.FileInfo[] zipfiles = zipdir.GetFiles();
                    foreach (System.IO.FileInfo file in zipfiles)
                    {
                        ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(file.FullName));

                        using (System.IO.FileStream fs = file.OpenRead())
                        {
                            int sourceBytes;
                            byte[] buffer = new byte[fs.Length];
                            entry.Size = fs.Length;
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            crc.Reset();
                            crc.Update(buffer);
                            entry.Crc = crc.Value;
                            zip.PutNextEntry(entry);
                            zip.Write(buffer, 0, sourceBytes);

                        }
                    }
                    zip.Finish();
                    zip.Close();
                }
                isZipSucess = true;
            }
            catch (Exception ex)
            {
                isZipSucess = false;
                LogUtil.Error(ex.ToString());
            }
            strFileName = strLocalPath;
            return isZipSucess;
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="strTargetFilePath">目标路径的压缩文件</param>
        /// <param name="strLocalPath">目标路径</param>
        public static bool UnZip(string strTargetFilePath, string directoryName)
        {
            bool isUnZipSucess = false;
            try
            {
                using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(strTargetFilePath)))
                {
                    ZipEntry theEntry;
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        string fileName = System.IO.Path.GetFileName(theEntry.Name);
                        // create directory
                        if (directoryName.Length > 0)
                        {
                            System.IO.Directory.CreateDirectory(directoryName);
                        }
                        if (fileName != String.Empty)
                        {
                            string strFilePath = directoryName + "\\" + theEntry.Name;//将文件解压缩到指定的目录
                            using (System.IO.FileStream streamWriter = System.IO.File.Create(strFilePath))
                            {
                                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;
                                    }
                                }
                            }
                        }
                    }
                }
                isUnZipSucess = true;
            }
            catch (Exception ex)
            {
                LogUtil.Error(ex.ToString());
                isUnZipSucess = false;
            }
            return isUnZipSucess;
        }
    }
 
 
posted @ 2012-11-02 17:01  巴别塔  阅读(826)  评论(0编辑  收藏  举报