導航:首頁 > 文件處理 > icsharpcode壓縮

icsharpcode壓縮

發布時間:2025-03-30 10:26:24

① 怎樣使用壓縮文件代碼

我在使用這個組件行,遇到了一個問題。
當壓縮小文件時沒有什麼錯誤,一旦源文件達到150M時,它會讓你的機器垮掉。(至少是我的機器)
為什麼會這樣,因為如果源文件是150M時,你就需要在內存申請一個150M大小的位元組數組。好點的機器還沒問題,一般的機器可就慘了。如果文件在大的話,好機器也受不了的。
為了解決大文件壓縮的問題,可以使用分段壓縮的方法。
private string CreateZIPFile(string path,int M)
{
try
{
Crc32 crc = new Crc32();
ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipout=new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(System.IO.File.Create(path+".zip"));
System.IO.FileStream fs=System.IO.File.OpenRead(path);
long pai=1024*1024*M;//每M兆寫一次
long forint=fs.Length/pai+1;
byte[] buffer=null;
ZipEntry entry = new ZipEntry(System.IO.Path.GetFileName(path));
entry.Size = fs.Length;
entry.DateTime = DateTime.Now;
zipout.PutNextEntry(entry);
for(long i=1;i<=forint;i++)
{
if(pai*i<fs.Length)
{
buffer = new byte[pai];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
else
{
if(fs.Length<pai)
{
buffer = new byte[fs.Length];
}
else
{
buffer = new byte[fs.Length-pai*(i-1)];
fs.Seek(pai*(i-1),System.IO.SeekOrigin.Begin);
}
}
fs.Read(buffer,0,buffer.Length);
crc.Reset();
crc.Update(buffer);
zipout.Write(buffer,0, buffer.Length);
zipout.Flush();
}
fs.Close();
zipout.Finish();
zipout.Close();
System.IO.File.Delete(path);
return path+".zip";
}
catch(Exception ex)
{
string str=ex.Message;
return path;
}
}

② 在C#里調用icsharpcode.sharpziplib解壓縮文件的問題

【【【【C#壓縮文件】】】】
方法1:
//【filepath想要壓縮文件芹笑的嫌游含地址】
//【zippath輸出壓縮文件的地址】
private void GetFileToZip(string filepath,string zippath)
{ FileStream fs = File.OpenRead(filepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close(); FileStream ZipFile = File.Create(zippath);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
ZipEntry ZipEntry = new ZipEntry(輸出的文件名稱);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6); ZipStream.Write(buffer, 0, buffer.Length);
ZipStream.Finish();
ZipStream.Close();
}
方法2:
private void FileToZip(string path,string address)
{
string name = openFileDialog1.FileName.Substring(openFileDialog1.FileName.LastIndexOf("\\")+1);
FileStream StreamToZip = new FileStream(path, FileMode.Open, FileAccess.Read);
FileStream ZipFile = File.Create(address);
ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);
//壓縮文件
ZipEntry ZipEntry = new ZipEntry(name);
ZipStream.PutNextEntry(ZipEntry);
ZipStream.SetLevel(6);
byte[] buffer = new byte[StreamToZip.Length];
StreamToZip.Read(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Write(buffer, 0, Convert.ToInt32(StreamToZip.Length));
ZipStream.Finish();
ZipStream.Close();
StreamToZip.Close(); }
【【【【【【C#解壓文件】磨彎】】】】】
private void ZipToFile(string path, string addres)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(path));
ZipEntry fileEntry;
while ((fileEntry = s.GetNextEntry()) != null)
{
string filename = Path.GetFileName(fileEntry.Name);
if (filename != "")
{
filename = addres + "\\" + filename;
FileStream streamWriter = File.Create(filename);
int size = 2048;
byte[] buffer = new byte[size]; size = s.Read(buffer, 0, size);
streamWriter.Write(buffer, 0, size);
streamWriter.Close();
}
}
s.Close();
}
【【【【【【C#壓縮目錄】】】】】】
方法1:
//【arg[0]要壓縮的目錄】
//【arg[1]輸出的壓縮文件】
private void DirectoryToZip(string path, string address)
{
//獲取當前文件夾中所有的文件
string[] filenames = Directory.GetFiles(path);
Crc32 crc = new Crc32();
//創建輸出文件(ZIP格式的文件)
ZipOutputStream zos = new ZipOutputStream(File.Create(address));
zos.SetLevel(6);
//遍歷所有的文件
foreach (string name in filenames)
{
FileStream fs = File.OpenRead(name);
byte[] buffer = new byte[fs.Length];
//讀取文件
fs.Read(buffer, 0, Convert.ToInt32(fs.Length));
//獲取文件的文件名稱和後綴名
string file = Path.GetFileName(name);
//輸出文件的名稱
ZipEntry entry = new ZipEntry(file);
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, Convert.ToInt32(fs.Length));
fs.Close();
}
zos.Finish();
zos.Close();
} 【【【【【【【【C#讀取壓縮文件(將壓縮文件轉換為二進制)】】】】】】】】
private void GetZipToByte(){
string path = @"C:\Documents and Settings\Administrator\桌面\文件.rar";
FileStream fs = new FileStream(path, FileMode.Open);
bytes = new byte[fs.Length];
int count = Convert.ToInt32(fs.Length);
fs.Read(bytes, 0, count);
}
【【【【【【【【C#將二進制轉換為壓縮文件】】】】】】】】
private void GetByteToZip()
{
string path = @"F:\dom.rar";//壓縮文件的地址
File.WriteAllBytes(path, bytes);
}

閱讀全文

與icsharpcode壓縮相關的資料

熱點內容
手機怎麼能徹底刪除所有app 瀏覽:463
超級解壓聲音完整版 瀏覽:218
網路游戲源碼怎麼查 瀏覽:661
js交互命令 瀏覽:757
linux與c語言腳本 瀏覽:244
java不可見字元 瀏覽:37
45秒廣告的閱讀app叫什麼 瀏覽:806
如何修改蘋果dns伺服器地址 瀏覽:168
婚後分居兩地怎麼解壓 瀏覽:585
時鍾同步命令 瀏覽:397
php消息列隊 瀏覽:971
用gcc編譯出現unknown 瀏覽:393
cmd命令保存 瀏覽:332
mysql查詢數組php 瀏覽:714
免備案雲伺服器網站 瀏覽:90
降龍伏虎源碼 瀏覽:902
phpresque丟數據 瀏覽:323
轉轉app閑置是什麼意思 瀏覽:361
美國程序員休假 瀏覽:786
洛克王國伺服器關了天梯怎麼辦 瀏覽:111