分享SharpZipLip使用代码
生活随笔
收集整理的這篇文章主要介紹了
分享SharpZipLip使用代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
zip類
public?class?ZipClass
????{
????????/**////?<summary>
????????///?壓縮方法
????????///?</summary>
????????///?<param?name="strPath">要壓縮文件夾</param>
????????///?<param?name="strFileName">生成的文件名</param>
????????///?<param?name="PassWord">密碼</param>
????????///?<returns>1?成功?-1輸入的壓縮文件夾為空?-2?輸入的壓縮文件夾目錄不存在!</returns>
????????public?static?int?Zip(string?strPath,?string?strFileName,?string?PassWord)
????????{
????????????if?(!String.IsNullOrEmpty(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾為空");
????????????????return?-1;
????????????}
????????????if?(!Directory.Exists(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾目錄不存在!");
????????????????return?-2;
????????????}
????????????if?(!File.Exists(strFileName))
????????????{
????????????????File.Create(strFileName);
????????????}
????????????ZipOutputStream?zip?=?new?ZipOutputStream(File.Create(strFileName));
????????????if?(!string.IsNullOrEmpty(PassWord))
????????????????zip.Password?=?PassWord;
????????????IList<FileInfo>?list?=?new?List<FileInfo>();
????????????GetFileList(strPath,?list);
????????????try
????????????{
????????????????byte[]?buffer?=?new?byte[2048];
????????????????int?count?=?0;
????????????????foreach?(FileInfo?fi?in?list)
????????????????{
????????????????????count?=?0;
????????????????????using?(FileStream?fs?=?File.OpenRead(fi.FullName))
????????????????????{
????????????????????????ZipEntry?entry?=?new?ZipEntry(fi.FullName.Substring(strPath.Length?+?1));
????????????????????????entry.DateTime?=?fi.LastWriteTime;
????????????????????????entry.Size?=?fs.Length;
????????????????????????zip.PutNextEntry(entry);
????????????????????????while?((count?=?fs.Read(buffer,?0,?2048))?>?0)
????????????????????????{
????????????????????????????zip.Write(buffer,?0,?count);
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????catch
????????????{
????????????????throw;
????????????}
????????????finally
????????????{
????????????????zip.Finish();
????????????????zip.Close();
????????????}
????????????return?1;
????????}
????????private?static?void?GetFileList(string?strPath,?IList<FileInfo>?list)
????????{
????????????DirectoryInfo?di?=?new?DirectoryInfo(strPath);
????????????foreach?(DirectoryInfo?di1?in?di.GetDirectories())
????????????{
????????????????GetFileList(di1.FullName,?list);
????????????}
????????????foreach?(FileInfo?fi?in?di.GetFiles())
????????????{
????????????????list.Add(fi);
????????????}
????????}
????????/**////?<summary>
????????///?解壓縮文件
????????///?</summary>
????????///?<param?name="strFileName">壓縮文件</param>
????????///?<param?name="strPath">目標目錄</param>
????????///?<param?name="PassWord">密碼</param>
????????///?<returns>1?成功?-1輸入的壓縮文件夾為空?-2?輸入的解壓縮文件夾目錄不存在!-3?文件不存在</returns>
????????public?static?int?UnZip(string?strFileName,?string?strPath,?string?PassWord)
????????{
????????????if?(!String.IsNullOrEmpty(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾為空");
????????????????return?-1;
????????????}
????????????if?(!Directory.Exists(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾目錄不存在!");
????????????????return?-2;
????????????}
????????????if?(!File.Exists(strFileName))
????????????{
????????????????//throw?new?Exception("文件:"?+?strFileName?+?"不存在!");
????????????????return?-3;
????????????}
????????????ZipEntry?entry;
????????????ZipInputStream?zis?=?null;
????????????try
????????????{
????????????????zis?=?new?ZipInputStream(File.Open(strFileName,?FileMode.Open));
????????????????if?(!string.IsNullOrEmpty(PassWord))
????????????????????zis.Password?=?PassWord;
????????????????byte[]?buffer?=?new?byte[2048];
????????????????int?count?=?0;
????????????????while?((entry?=?zis.GetNextEntry())?!=?null)
????????????????{
????????????????????CreateDirList(entry.Name,?strPath);
????????????????????string?strPath1?=?strPath?+?"\\"?+?entry.Name;
????????????????????using?(FileStream?streamWriter?=?File.Create(strPath1))
????????????????????{
????????????????????????while?((count?=?zis.Read(buffer,?0,?2048))?>?0)
????????????????????????{
????????????????????????????streamWriter.Write(buffer,?0,?count);
????????????????????????}
????????????????????}
????????????????????File.SetLastWriteTime(strPath1,?entry.DateTime);
????????????????}
????????????}
????????????catch
????????????{
????????????????throw;
????????????}
????????????finally
????????????{
????????????????if?(zis?!=?null)
????????????????????zis.Close();
????????????}
????????????return?1;
????????}
????????private?static?void?CreateDirList(string?filename,?string?basePath)
????????{
????????????string?dirName?=?basePath;
????????????string[]?dirlevelname?=?filename.Split('\\');
????????????for?(int?i?=?0;?i?<?dirlevelname.Length?-?1;?i++)
????????????{
????????????????dirName?+=?"\\"?+?dirlevelname[i];
????????????????if?(Directory.Exists(dirName))
????????????????{
????????????????????continue;
????????????????}
????????????????Directory.CreateDirectory(dirName);
????????????}
????????}
????} 沒有什么說的,直接上代碼.
public?class?ZipClass
????{
????????/**////?<summary>
????????///?壓縮方法
????????///?</summary>
????????///?<param?name="strPath">要壓縮文件夾</param>
????????///?<param?name="strFileName">生成的文件名</param>
????????///?<param?name="PassWord">密碼</param>
????????///?<returns>1?成功?-1輸入的壓縮文件夾為空?-2?輸入的壓縮文件夾目錄不存在!</returns>
????????public?static?int?Zip(string?strPath,?string?strFileName,?string?PassWord)
????????{
????????????if?(!String.IsNullOrEmpty(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾為空");
????????????????return?-1;
????????????}
????????????if?(!Directory.Exists(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾目錄不存在!");
????????????????return?-2;
????????????}
????????????if?(!File.Exists(strFileName))
????????????{
????????????????File.Create(strFileName);
????????????}
????????????ZipOutputStream?zip?=?new?ZipOutputStream(File.Create(strFileName));
????????????if?(!string.IsNullOrEmpty(PassWord))
????????????????zip.Password?=?PassWord;
????????????IList<FileInfo>?list?=?new?List<FileInfo>();
????????????GetFileList(strPath,?list);
????????????try
????????????{
????????????????byte[]?buffer?=?new?byte[2048];
????????????????int?count?=?0;
????????????????foreach?(FileInfo?fi?in?list)
????????????????{
????????????????????count?=?0;
????????????????????using?(FileStream?fs?=?File.OpenRead(fi.FullName))
????????????????????{
????????????????????????ZipEntry?entry?=?new?ZipEntry(fi.FullName.Substring(strPath.Length?+?1));
????????????????????????entry.DateTime?=?fi.LastWriteTime;
????????????????????????entry.Size?=?fs.Length;
????????????????????????zip.PutNextEntry(entry);
????????????????????????while?((count?=?fs.Read(buffer,?0,?2048))?>?0)
????????????????????????{
????????????????????????????zip.Write(buffer,?0,?count);
????????????????????????}
????????????????????}
????????????????}
????????????}
????????????catch
????????????{
????????????????throw;
????????????}
????????????finally
????????????{
????????????????zip.Finish();
????????????????zip.Close();
????????????}
????????????return?1;
????????}
????????private?static?void?GetFileList(string?strPath,?IList<FileInfo>?list)
????????{
????????????DirectoryInfo?di?=?new?DirectoryInfo(strPath);
????????????foreach?(DirectoryInfo?di1?in?di.GetDirectories())
????????????{
????????????????GetFileList(di1.FullName,?list);
????????????}
????????????foreach?(FileInfo?fi?in?di.GetFiles())
????????????{
????????????????list.Add(fi);
????????????}
????????}
????????/**////?<summary>
????????///?解壓縮文件
????????///?</summary>
????????///?<param?name="strFileName">壓縮文件</param>
????????///?<param?name="strPath">目標目錄</param>
????????///?<param?name="PassWord">密碼</param>
????????///?<returns>1?成功?-1輸入的壓縮文件夾為空?-2?輸入的解壓縮文件夾目錄不存在!-3?文件不存在</returns>
????????public?static?int?UnZip(string?strFileName,?string?strPath,?string?PassWord)
????????{
????????????if?(!String.IsNullOrEmpty(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾為空");
????????????????return?-1;
????????????}
????????????if?(!Directory.Exists(strPath))
????????????{
????????????????//throw?new?Exception("輸入的壓縮文件夾目錄不存在!");
????????????????return?-2;
????????????}
????????????if?(!File.Exists(strFileName))
????????????{
????????????????//throw?new?Exception("文件:"?+?strFileName?+?"不存在!");
????????????????return?-3;
????????????}
????????????ZipEntry?entry;
????????????ZipInputStream?zis?=?null;
????????????try
????????????{
????????????????zis?=?new?ZipInputStream(File.Open(strFileName,?FileMode.Open));
????????????????if?(!string.IsNullOrEmpty(PassWord))
????????????????????zis.Password?=?PassWord;
????????????????byte[]?buffer?=?new?byte[2048];
????????????????int?count?=?0;
????????????????while?((entry?=?zis.GetNextEntry())?!=?null)
????????????????{
????????????????????CreateDirList(entry.Name,?strPath);
????????????????????string?strPath1?=?strPath?+?"\\"?+?entry.Name;
????????????????????using?(FileStream?streamWriter?=?File.Create(strPath1))
????????????????????{
????????????????????????while?((count?=?zis.Read(buffer,?0,?2048))?>?0)
????????????????????????{
????????????????????????????streamWriter.Write(buffer,?0,?count);
????????????????????????}
????????????????????}
????????????????????File.SetLastWriteTime(strPath1,?entry.DateTime);
????????????????}
????????????}
????????????catch
????????????{
????????????????throw;
????????????}
????????????finally
????????????{
????????????????if?(zis?!=?null)
????????????????????zis.Close();
????????????}
????????????return?1;
????????}
????????private?static?void?CreateDirList(string?filename,?string?basePath)
????????{
????????????string?dirName?=?basePath;
????????????string[]?dirlevelname?=?filename.Split('\\');
????????????for?(int?i?=?0;?i?<?dirlevelname.Length?-?1;?i++)
????????????{
????????????????dirName?+=?"\\"?+?dirlevelname[i];
????????????????if?(Directory.Exists(dirName))
????????????????{
????????????????????continue;
????????????????}
????????????????Directory.CreateDirectory(dirName);
????????????}
????????}
????} 沒有什么說的,直接上代碼.
轉載于:https://www.cnblogs.com/LifelongLearning/archive/2008/01/03/1025131.html
總結
以上是生活随笔為你收集整理的分享SharpZipLip使用代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Don't Laugh!I'm An E
- 下一篇: 梦到被黄牛追是什么意思