⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plugin.cs

📁 自己写的可扩展插件文件管理器
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using Core.Services.Sdk;
using ICSharpCode.SharpZipLib.BZip2;
using System.IO;
using System.Windows.Forms;


namespace CompressionLib
{

    [Plugin(true)]
    public class ZipCompress:IPlugin
    {
        private PluginInfo p;
        FileStream fs, ofs;

        private int FileLen;
        private string PrevFileName;

        const int FIXED_HEADER_LEN = 24;//文件头定长,不写入文件

        private byte[] HEADER = { 0x04, 0x03, 0x6b, 0x2e };//表示文件为bip文件,4bytes
       // private byte FLAG=0x0;//标识文件是否已压缩

        private int FILE_LEN;//FILE块长度,4bytes
        private byte[] FILE;//原文件名(Unicode)
        private byte[] HASH;//DATA块的哈希值,16bytes

        private void WriteHeader(FileStream targetFile)//写入文件头
        {
            targetFile.Seek(0, SeekOrigin.Begin);
            BinaryWriter bw = new BinaryWriter(targetFile);
            bw.Write(HEADER);
            bw.Write(FILE_LEN);
            bw.Write(FILE);
            bw.Write(HASH);
        }

        private bool ReadHeader(FileStream targetFile)//检查文件头
        {
            byte[] rHEADER = new byte[4];
            byte[] cHASH = new byte[16];
            byte[] rHASH = new byte[16];
            int rFILE_LEN;
            BinaryReader br = new BinaryReader(targetFile);
            rHEADER = br.ReadBytes(4);
            if ((rHEADER[0] != HEADER[0]) || (rHEADER[1] != HEADER[1]) || (rHEADER[2] != HEADER[2]) || (rHEADER[3] != HEADER[3]))
                return false;
            rFILE_LEN = br.ReadInt32();
            byte[] rFILE = new byte[rFILE_LEN];
            rFILE = br.ReadBytes(rFILE_LEN);
            rHASH = br.ReadBytes(16);
            System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
            cHASH = md.ComputeHash(br.ReadBytes((int)(br.BaseStream.Length - FIXED_HEADER_LEN - rFILE.Length)));
            int i = 0;
            foreach (byte b in cHASH)
            {
                if (b != rHASH[i])
                    return false;
                else i++;
            }
            PrevFileName = Encoding.Unicode.GetString(rFILE);
            FileLen = rFILE_LEN;
            return true;
        }

        public ZipCompress()
        {
            p = new PluginInfo("bZip Compress", "Signergenix", "使用bZIP压缩/解压文件", new Version(1, 0, 3204, 24355), new ToolStripMenuItem("bZip压缩/解压"));
        }

        #region IPlugin 成员

        public void AssignTask(System.Collections.ArrayList Targets)
        {
            int curr=0,max=0;
            foreach (string s in Targets)
            {
                max += (int)(new FileInfo(s).Length);
            }
            foreach (string s in Targets)
            {
                fs=new FileStream(s,FileMode.Open);
                if (ReadHeader(fs))//如果文件头正确就解压缩
                {
                    Directory.SetCurrentDirectory(new FileInfo(s).DirectoryName);
                    byte[] buffer = new byte[fs.Length - (FIXED_HEADER_LEN + FileLen)];
                    fs.Position = FIXED_HEADER_LEN + FileLen;
                    fs.Read(buffer, 0, buffer.Length);
                    curr += (int)fs.Length;
                    fs.Close();
                    MemoryStream ms = new MemoryStream(buffer);
                    ofs = new FileStream(PrevFileName, FileMode.Create);
                    BZip2.Decompress(ms, ofs);
                    OnProgressChanged(new ProgressChangedEventArgs(curr, max));
                }
                else//不正确就压缩
                {
                    fs.Position = 0;
                    System.Security.Cryptography.MD5CryptoServiceProvider md = new System.Security.Cryptography.MD5CryptoServiceProvider();
                    ofs = new FileStream(s.Remove(s.LastIndexOf(".")) + ".bip", FileMode.Create);
                    curr += (int)fs.Length;
                    BZip2.Compress(fs, ofs, 512);
                    ofs = new FileStream(s.Remove(s.LastIndexOf(".")) + ".bip", FileMode.Open);
                    byte[] buff = new byte[ofs.Length];
                    ofs.Read(buff, 0, buff.Length);
                    HASH = md.ComputeHash(buff);
                    FILE = Encoding.Unicode.GetBytes(new FileInfo(s).Name);
                    FILE_LEN = FILE.Length;
                    WriteHeader(ofs);
                    ofs.Write(buff, 0, buff.Length);
                    ofs.Close();
                    OnProgressChanged(new ProgressChangedEventArgs(curr, max));
                }
            }
        }

        public PluginInfo PluginInfo
        {
            get { return p; }
        }

        public event ProgressChangedEventHandler ProgressChanged;

        protected virtual void OnProgressChanged(ProgressChangedEventArgs e)
        {
            if (ProgressChanged != null)
            {
                ProgressChanged(this, e);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            if (fs != null)
            {
                fs.Dispose();
                ofs.Dispose();
            }
        }

        #endregion

        #region IPlugin 成员


        public Form Window
        {
            get { return null; }
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -