资源简介

本程序是基于 sharpcompress-master 设计的演示程序。
开原地址:https://github.com/adamhathcock/sharpcompress。
1. 程序主要展示了压缩文件的解压缩,支持的文件类型有:rar、zip、7zip、Gzip、tar......等压缩格式,展示了密码解压、分卷解压、解压进度等功能。
2. 不支持“带密码的分卷压缩文件”的解压;无密码分卷压缩、单个带密码文件正常解压。
3. 此程序在设计中,对组件源代码进行了修改,与原组件代码对照查看。
4、sharpcompress-master组件功能繁多,比如,文件压缩为图片、压缩文件内文件添加删除、更名等等,对照现有的压缩软件,功能均具备,可在代码中查看使用。本示例只是展示了解压缩功能。

此程序为识别全部压缩格式,未引用DLL,全部为cs文件,如有错误,自行对照修正。




private void Uncompress(string archiveFile, string destinationPath, string password = null)
        {
            var archive = ArchiveFactory.Open(archiveFile, new ReaderOptions()
            {
                Password = password,
                LeaveStreamOpen = true
            });
            volumes = archive.Entries.Count();

            archive.EntryExtractionBegin  = archive_EntryExtractionBegin;
            archive.FilePartExtractionBegin  = archive_FilePartExtractionBegin;
            archive.CompressedBytesRead  = archive_CompressedBytesRead;

            Task.Run(() =>
            {
                long order = 0;
                foreach (var entry in archive.Entries)
                {
                    if (!entry.IsDirectory)
                    {
                        entry.WriteToDirectory(destinationPath, new ExtractionOptions()
                        {
                            ExtractFullPath = true,
                            Overwrite = true,
                        });
                    }
                    order  ;
                    this.CrossThreadCalls(() =>
                    {
                        if (volumes > 1)
                        {
                            progress[0].Value = CreatePercentage(order, volumes);
                        }
                    });
                }
                MessageBox.Show("解压缩任务完成 !");
            });
        }

        private void archive_EntryExtractionBegin(object sender, ArchiveExtractionEventArgs<IArchiveEntry> e)
        {
            entryTotal = e.Item.Size;
            info_builde1 = "提取文件: "   e.Item.Key   ";\r";
        }

        private void archive_FilePartExtractionBegin(object sender, FilePartExtractionBeginEventArgs e)
        {
            partTotal = e.Size;
            info_builde2 = "分卷:"   e.Name   ";\r";
        }

        private void archive_CompressedBytesRead(object sender, CompressedBytesReadEventArgs e)
        {
            string percentage = entryTotal.HasValue ? CreatePercentage(e.CompressedBytesRead, entryTotal.Value).ToString() : "Unknown";
            info_builde = info_builde1   info_builde2   "字节:"   e.CompressedBytesRead   ";百分比:"   percentage   "%";
            this.CrossThreadCalls(() =>
            {
                compress_Info.Text = info_builde;
                progress[1].Value = int.Parse(percentage) > 100 || int.Parse(percentage) < 0 ? 100 : int.Parse(percentage);
                if (volumes < 2) progress[0].Value = progress[1].Value;
                Application.DoEvents();
            });
        }

        private int CreatePercentage(long n, long d)
        {
            return (int)(((double)n / (double)d) * 100);
        }

资源截图

代码片段和文件信息

using System.Threading;
using System.Windows.Forms;

public static class CrossThreadCall
{
    public static void CrossThreadCalls(this Control ctl ThreadStart del)
    {
        if (del == null) return;
        if (ctl.InvokeRequired)  ctl.Invoke(del null);
        else del();
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.    129024  2019-07-21 21:18  compress_Demo\.vs\compress_Demo\v15\.suo

     文件          0  2019-07-21 06:22  compress_Demo\.vs\compress_Demo\v15\Server\sqlite3\db.lock

     文件    1523712  2019-07-21 21:02  compress_Demo\.vs\compress_Demo\v15\Server\sqlite3\storage.ide

     文件      32768  2019-07-21 20:15  compress_Demo\.vs\compress_Demo\v15\Server\sqlite3\storage.ide-shm

     文件    4449632  2019-07-21 21:11  compress_Demo\.vs\compress_Demo\v15\Server\sqlite3\storage.ide-wal

     文件        189  2019-07-21 06:22  compress_Demo\compress_Demo\App.config

     文件     813568  2019-07-21 21:11  compress_Demo\compress_Demo\bin\Debug\compress_Demo.exe

     文件      28717  2019-07-21 21:01  compress_Demo\compress_Demo\compress_Demo.csproj

     文件       2352  2019-06-28 15:48  compress_Demo\compress_Demo\contextUI\ConextControl.cs

     文件        644  2019-07-21 21:02  compress_Demo\compress_Demo\contextUI\ConextDrawText.cs

     文件      64180  2019-07-21 07:14  compress_Demo\compress_Demo\contextUI\ConextFontAwesome.cs

     文件       5771  2019-06-23 09:32  compress_Demo\compress_Demo\contextUI\ConextForm.cs

     文件        304  2019-07-20 11:55  compress_Demo\compress_Demo\CrossThreadCall.cs

     文件      16052  2019-07-21 21:11  compress_Demo\compress_Demo\Form1.cs

     文件        525  2019-07-21 06:22  compress_Demo\compress_Demo\Program.cs

     文件       1324  2019-07-21 06:22  compress_Demo\compress_Demo\Properties\AssemblyInfo.cs

     文件       2839  2019-07-21 06:22  compress_Demo\compress_Demo\Properties\Resources.Designer.cs

     文件       5612  2019-07-21 06:22  compress_Demo\compress_Demo\Properties\Resources.resx

     文件       1100  2019-07-21 06:22  compress_Demo\compress_Demo\Properties\Settings.Designer.cs

     文件        249  2019-07-21 06:22  compress_Demo\compress_Demo\Properties\Settings.settings

     文件       5831  2019-07-21 07:15  compress_Demo\compress_Demo\Resource.Designer.cs

     文件      11950  2019-07-21 07:15  compress_Demo\compress_Demo\Resource.resx

     文件     165548  2019-07-21 07:10  compress_Demo\compress_Demo\Resources\fontawesome-webfont.ttf

     文件       7071  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\AbstractArchive.cs

     文件       5103  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\AbstractWritableArchive.cs

     文件       5596  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\ArchiveFactory.cs

     文件       6226  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\GZip\GZipArchive.cs

     文件        901  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\GZip\GZipArchiveEntry.cs

     文件       1866  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\GZip\GZipWritableArchiveEntry.cs

     文件       1757  2019-06-27 04:16  compress_Demo\compress_Demo\SharpCompress\Archives\IArchive.cs

............此处省略391个文件信息

评论

共有 条评论