免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版
打開APP
未登錄
開通VIP,暢享免費電子書等14項超值服
開通VIP
首頁
好書
留言交流
下載APP
聯(lián)系客服
C#壓縮解壓zip 文件
NaturalWill
>《待分類1》
2014.08.29
關(guān)注
[c-sharp]
view plain
copy
/// <summary>
/// Zip 壓縮文件
/// </summary>
public class Zip
{
public Zip()
{
}
#region 加壓方法
/// <summary>
/// 功能:壓縮文件(暫時只壓縮文件夾下一級目錄中的文件,文件夾及其子級被忽略)
/// </summary>
/// <param name="dirPath">被壓縮的文件夾夾路徑</param>
/// <param name="zipFilePath">生成壓縮文件的路徑,為空則默認(rèn)與被壓縮文件夾同一級目錄,名稱為:文件夾名+.zip</param>
/// <param name="err">出錯信息</param>
/// <returns>是否壓縮成功</returns>
public static bool ZipFile(string dirPath, string zipFilePath, out string err)
{
err = "";
if (dirPath == string.Empty)
{
err = "要壓縮的文件夾不能為空!";
return false;
}
if (!Directory.Exists(dirPath))
{
err = "要壓縮的文件夾不存在!";
return false;
}
//壓縮文件名為空時使用文件夾名+.zip
if (zipFilePath == string.Empty)
{
if (dirPath.EndsWith("http://"))
{
dirPath = dirPath.Substring(0, dirPath.Length - 1);
}
zipFilePath = dirPath + ".zip";
}
try
{
string[] filenames = Directory.GetFiles(dirPath);
using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
{
s.SetLevel(9);
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(file));
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
s.Finish();
s.Close();
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}
#endregion
#region 解壓
/// <summary>
/// 功能:解壓zip格式的文件。
/// </summary>
/// <param name="zipFilePath">壓縮文件路徑</param>
/// <param name="unZipDir">解壓文件存放路徑,為空時默認(rèn)與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾</param>
/// <param name="err">出錯信息</param>
/// <returns>解壓是否成功</returns>
public static bool UnZipFile(string zipFilePath, string unZipDir, out string err)
{
err = "";
if (zipFilePath == string.Empty)
{
err = "壓縮文件不能為空!";
return false;
}
if (!File.Exists(zipFilePath))
{
err = "壓縮文件不存在!";
return false;
}
//解壓文件夾為空時默認(rèn)與壓縮文件同一級目錄下,跟壓縮文件同名的文件夾
if (unZipDir == string.Empty)
unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
if (!unZipDir.EndsWith("http://"))
unZipDir += "http://";
if (!Directory.Exists(unZipDir))
Directory.CreateDirectory(unZipDir);
try
{
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
if (directoryName.Length > 0)
{
Directory.CreateDirectory(unZipDir + directoryName);
}
if (!directoryName.EndsWith("http://"))
directoryName += "http://";
if (fileName != String.Empty)
{
using (FileStream streamWriter = File.Create(unZipDir + 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;
}
}
}
}
}//while
}
}
catch (Exception ex)
{
err = ex.Message;
return false;
}
return true;
}//解壓結(jié)束
#endregion
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報
。
打開APP,閱讀全文并永久保存
查看更多類似文章
猜你喜歡
類似文章
文件夾操作及zip的子文件讀取,解壓-文件下載
NW.JS教程(二) 本地文件的操作及process進(jìn)程 | 顧陌 | Blog
c++統(tǒng)計文件行數(shù)
golang判斷是文件還是文件夾
[轉(zhuǎn)]C# FTP上傳文件及文件夾至服務(wù)器代碼
使用commons
更多類似文章 >>
生活服務(wù)
首頁
萬象
文化
人生
生活
健康
教育
職場
理財
娛樂
藝術(shù)
上網(wǎng)
留言交流
回頂部
聯(lián)系我們
分享
收藏
點擊這里,查看已保存的文章
導(dǎo)長圖
關(guān)注
一鍵復(fù)制
下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!
聯(lián)系客服
微信登錄中...
請勿關(guān)閉此頁面
先別劃走!
送你5元優(yōu)惠券,購買VIP限時立減!
5
元
優(yōu)惠券
優(yōu)惠券還有
10:00
過期
馬上使用
×