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

📄 frmzip.cs

📁 这是一款系统系统备份的软件
💻 CS
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
using System.Threading;

namespace ZipApp
{
    public partial class FrmZip : Form
    {
        public FrmZip()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.ShowDialog();
            this.txtZip.Text = file.FileName;
        }

        private void btnZip_Click(object sender, EventArgs e)
        {
            ZipDao dao = new ZipDao(this.txtZip.Text);
            Thread myTh = new Thread(new ThreadStart(dao.ZipFold));
            myTh.Start();
        }


        private void ZipFile()
        {
            //获得压缩的文件
            string file = this.txtZip.Text;
            //压缩文件的名字
            string name = file.Replace(file.Substring(file.LastIndexOf(".")), ".rar");
            //压缩文件的流对象
            // MessageBox.Show(name);
            ZipOutputStream output = new ZipOutputStream(File.Create(name));
            output.SetLevel(6);
            FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] bt = new byte[fs.Length];
            fs.Read(bt, 0, bt.Length);
            //存储要压缩的文件
            ZipEntry entry = new ZipEntry(file);
            entry.Size = fs.Length;
            entry.DateTime = DateTime.Now;
            fs.Close();
            //存放文件数据
            Crc32 crc = new Crc32();
            crc.Reset();  //清除crc内容
            crc.Update(bt);  //更新文件内容到crc中
            entry.Crc = crc.Value;   //将文件内容放到压缩文件中
            output.PutNextEntry(entry);
            //将数据写入压缩流中
            output.Write(bt, 0, bt.Length);
            output.Close();
            MessageBox.Show("压缩成功");
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fold = new FolderBrowserDialog();
            fold.ShowDialog();
            this.txtZip.Text = fold.SelectedPath;
        }

        private void btnSel_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.ShowDialog();
            string path = file.FileName;
            this.txtUZip.Text = path;
            this.txtPath.Text = path.Substring(0, path.LastIndexOf("\\"));
        }

        private void btnS_Click(object sender, EventArgs e)
        {
            string file = this.txtUZip.Text;
            //读压缩文件内容流
            ZipInputStream input = new ZipInputStream(File.OpenRead(file));
            ZipEntry entry = input.GetNextEntry();
            //要存放的路径
            string dir = this.txtPath.Text;
            while (entry != null)
            {
                string filename = entry.Name;
                //读压缩文件内容
                //FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                int size = 2048;
                byte[] bt = new byte[size];
                FileStream inpuFs = File.Create(dir + filename.Substring(filename.LastIndexOf("\\")));
                while (true)
                {
                    size = input.Read(bt, 0, bt.Length);
                    if (size > 0)
                    {
                        inpuFs.Write(bt, 0, size);
                    }
                    else
                    {
                        inpuFs.Close();
                        break;
                    }
                }
                entry = input.GetNextEntry();
            }
            input.Close();
        }
    }
}

⌨️ 快捷键说明

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