免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
關(guān)于C#對(duì)文件的壓縮和解壓縮的問(wèn)題

項(xiàng)目中用到壓縮和解壓的功能,在網(wǎng)上查查看,有如下幾種,整理一下,供參考
: 引用第三方的插件實(shí)現(xiàn)
在引用中添加ICSharpCode.SharpZipLib.dll文件

可從www.icsharpcode.net/Opensource/SharpZipLib/Download.aspx獲得

需要添加以下引用

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.GZip;


對(duì)文件壓縮的方法如下:

 // 對(duì)目標(biāo)文件夾進(jìn)行壓縮,將壓縮結(jié)果保存為指定文件

 //FilePath存放的是目標(biāo)文件夾下要壓縮的文件BakPath 壓縮后文件存放的路徑

public class ZipClass
 {
  
           public ArrayList Zip(string FilePath, string BakPath)

  {
   // 新建一個(gè)list數(shù)組,用于存儲(chǔ)壓縮后的文件的名字
   ArrayList list = new ArrayList();
   try
   {

    string[] filenames = Directory.GetFiles(FilePath);
    
    list.Add(filenames);
    Crc32 crc = new Crc32();//新建Checksums的Crc32類(lèi)對(duì)象

    ZipOutputStream ZipStream = new ZipOutputStream(File.Create(BakPath));

    foreach (string file in filenames)

    {

     //打開(kāi)要壓縮的文件

     FileStream fs = File.OpenRead(file);

     byte[] buffer = new byte[fs.Length];
    
     fs.Read(buffer, 0, buffer.Length);

     //獲取壓縮文件的相對(duì)路徑

     string files = file.Substring(file.LastIndexOf("\\"));
     ZipEntry entry = new ZipEntry(files);

     entry.DateTime = DateTime.Now;

     entry.Size = fs.Length;

     fs.Close();

     crc.Reset();

     crc.Update(buffer);

     entry.Crc = crc.Value;

     ZipStream.PutNextEntry(entry);

     ZipStream.Write(buffer, 0, buffer.Length);

    }

    ZipStream.Finish();

    ZipStream.Close();
    return list;
   }
   catch (Exception ee)
   {
    throw ee;            
   }

  }

 

在解壓縮類(lèi)中添加如下引用

using ICSharpCode.SharpZipLib.Zip;


對(duì)文件的解壓縮的方法如下:

public void UnZip(string[] args)

{

     ZipInputStream s = new ZipInputStream(File.OpenRead(args[0]));

ZipEntry theEntry;

     while ((theEntry = s.GetNextEntry()) != null)

     {

string directoryName = Path.GetDirectoryName(args[1]);

string fileName = Path.GetFileName(theEntry.Name);

         //生成解壓目錄

          Directory.CreateDirectory(directoryName);

if (fileName != String.Empty)

          {

            //解壓文件到指定的目錄

             FileStream streamWriter = File.Create(args[1] + 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();

        }
. Net 2.0中的自帶的GZipStream類(lèi)


 
在壓縮和解壓縮的時(shí)候只要添加如下引用

 using System.IO.Compression;
對(duì)文件壓縮方法如下:

public void CompressFile(string sourceFile, string destinationFile)

{

if (File.Exists(sourceFile) == false) //判斷文件是否存在

       throw new FileNotFoundException();

//創(chuàng)建文件流和字節(jié)數(shù)組

byte[] buffer = null;

FileStream sourceStream = null;

FileStream destinationStream = null;

GZipStream compressedStream = null;

   try

{

     sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read);

     buffer = new byte[sourceStream.Length];

//把文件流存放到字節(jié)數(shù)組中

      int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);

if (checkCounter != buffer.Length)

        {

             throw new ApplicationException();

         }

destinationStream = new FileStream(destinationFile, FileMode.OpenOrCreate, FileAccess.Write);

//創(chuàng)建GzipStream實(shí)例,寫(xiě)入壓縮的文件流

        compressedStream = new GZipStream(destinationStream, CompressionMode.Compress, true);

         compressedStream.Write(buffer, 0, buffer.Length);

      }

     catch (ApplicationException ex)

      {

          MessageBox.Show(ex.Message, "壓縮文件時(shí)發(fā)生錯(cuò)誤:", MessageBoxButtons.OK, MessageBoxIcon.Error);

       }

       finally

      {

           // Make sure we allways close all streams

           if (sourceStream != null)

               {     sourceStream.Close();}

           if (compressedStream != null)

                {    compressedStream.Close();}

           if (destinationStream != null)

                {    destinationStream.Close();}

       }

}


解壓縮方法如下:

  public void DecompressFile(string sourceFile, string destinationFile)

  {

       if (File.Exists(sourceFile) == false)

             throw new FileNotFoundException();

        FileStream sourceStream = null;

        FileStream destinationStream = null;

        GZipStream decompressedStream = null;

        byte[] quartetBuffer = null;

        try

        {

         sourceStream = new FileStream(sourceFile, FileMode.Open);

         decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true);

         quartetBuffer = new byte[4];

int position = (int)sourceStream.Length - 4;

sourceStream.Position = position;

sourceStream.Read(quartetBuffer, 0, 4);

sourceStream.Position = 0;

int checkLength = BitConverter.ToInt32(quartetBuffer, 0);

byte[] buffer = new byte[checkLength + 100];

int offset = 0;

int total = 0;

          // 從字節(jié)數(shù)組中讀取壓縮數(shù)據(jù)

         while (true)

           {

                    int bytesRead = decompressedStream.Read(buffer, offset, 100);

                    if (bytesRead == 0) break;

                    offset += bytesRead;

                    total += bytesRead;

             }

            destinationStream = new FileStream(destinationFile, FileMode.Create);

            destinationStream.Write(buffer, 0, total);

             destinationStream.Flush();

          }

          catch (ApplicationException ex)

          {

              MessageBox.Show(ex.Message, "解壓文件時(shí)發(fā)生錯(cuò)誤:", MessageBoxButtons.OK, MessageBoxIcon.Error);

          }

          finally

          {              

                if (sourceStream != null)

                   { sourceStream.Close();}

                if (decompressedStream != null)

                   { decompressedStream.Close();}

                if (destinationStream != null)

                  {  destinationStream.Close();}

         }

       }

比較:

 引用 SharpZipLib的方法通用性比較強(qiáng),能壓縮和對(duì)別的解壓文件進(jìn)行解壓;

 GzipStream類(lèi)對(duì)文件進(jìn)行壓縮和解壓比較方便,但是其只能對(duì)自己的壓縮文件進(jìn)行解壓,對(duì)常見(jiàn)的壓縮格式不支持
粗略的總結(jié)了一下,有錯(cuò)誤的地方還望多多指出. ^_^

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
C# 壓縮文件(System.IO.Compression)
【新提醒】Unity 游戲資源更新
.net 壓縮文件夾
C#線程系列講座(3):線程池和文件下載服務(wù)器
上傳圖片檢測(cè)其是否為真實(shí)的圖片 防范病毒上傳至服務(wù)器
C# TcpClient網(wǎng)絡(luò)編程傳輸文件(帶文件名)
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服