.NET压缩文件 (可多层目录)

FrmZip.designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FrmZip
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.tbDirToZip = New System.Windows.Forms.TextBox
        Me.tbZipToCreate = New System.Windows.Forms.TextBox
        Me.btnDirBrowse = New System.Windows.Forms.Button
        Me.btnZipUp = New System.Windows.Forms.Button
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        Me.ProgressBar2 = New System.Windows.Forms.ProgressBar
        Me.lblStatus = New System.Windows.Forms.Label
        Me.btnCancel = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(11, 13)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(78, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "directory to zip:"
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(11, 39)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(84, 13)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "zip file to create:"
        '
        'tbDirToZip
        '
        Me.tbDirToZip.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbDirToZip.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest
        Me.tbDirToZip.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystemDirectories
        Me.tbDirToZip.Location = New System.Drawing.Point(107, 9)
        Me.tbDirToZip.Name = "tbDirToZip"
        Me.tbDirToZip.Size = New System.Drawing.Size(327, 20)
        Me.tbDirToZip.TabIndex = 2
        '
        'tbZipToCreate
        '
        Me.tbZipToCreate.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.tbZipToCreate.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest
        Me.tbZipToCreate.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem
        Me.tbZipToCreate.Location = New System.Drawing.Point(106, 35)
        Me.tbZipToCreate.Name = "tbZipToCreate"
        Me.tbZipToCreate.Size = New System.Drawing.Size(327, 20)
        Me.tbZipToCreate.TabIndex = 6
        '
        'btnDirBrowse
        '
        Me.btnDirBrowse.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnDirBrowse.Location = New System.Drawing.Point(440, 10)
        Me.btnDirBrowse.Name = "btnDirBrowse"
        Me.btnDirBrowse.Size = New System.Drawing.Size(32, 19)
        Me.btnDirBrowse.TabIndex = 4
        Me.btnDirBrowse.Text = "..."
        Me.btnDirBrowse.UseVisualStyleBackColor = True
        '
        'btnZipUp
        '
        Me.btnZipUp.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnZipUp.Location = New System.Drawing.Point(377, 72)
        Me.btnZipUp.Name = "btnZipUp"
        Me.btnZipUp.Size = New System.Drawing.Size(94, 23)
        Me.btnZipUp.TabIndex = 0
        Me.btnZipUp.Text = "Zip It!"
        Me.btnZipUp.UseVisualStyleBackColor = True
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar1.Location = New System.Drawing.Point(12, 105)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(459, 13)
        Me.ProgressBar1.TabIndex = 6
        '
        'ProgressBar2
        '
        Me.ProgressBar2.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ProgressBar2.Location = New System.Drawing.Point(13, 124)
        Me.ProgressBar2.Name = "ProgressBar2"
        Me.ProgressBar2.Size = New System.Drawing.Size(459, 13)
        Me.ProgressBar2.TabIndex = 7
        '
        'lblStatus
        '
        Me.lblStatus.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.lblStatus.AutoSize = True
        Me.lblStatus.Location = New System.Drawing.Point(11, 148)
        Me.lblStatus.Name = "lblStatus"
        Me.lblStatus.Size = New System.Drawing.Size(16, 13)
        Me.lblStatus.TabIndex = 8
        Me.lblStatus.Text = "..."
        '
        'btnCancel
        '
        Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.btnCancel.Enabled = False
        Me.btnCancel.Location = New System.Drawing.Point(277, 72)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(94, 23)
        Me.btnCancel.TabIndex = 8
        Me.btnCancel.Text = "Cancel"
        Me.btnCancel.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(484, 172)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.lblStatus)
        Me.Controls.Add(Me.ProgressBar2)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.btnZipUp)
        Me.Controls.Add(Me.btnDirBrowse)
        Me.Controls.Add(Me.tbZipToCreate)
        Me.Controls.Add(Me.tbDirToZip)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.MinimumSize = New System.Drawing.Size(500, 208)
        Me.Name = "Form1"
        Me.Text = "DotNetZip WinForms VB Zip Creator"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents tbDirToZip As System.Windows.Forms.TextBox
    Friend WithEvents tbZipToCreate As System.Windows.Forms.TextBox
    Friend WithEvents btnDirBrowse As System.Windows.Forms.Button
    Friend WithEvents btnZipUp As System.Windows.Forms.Button
    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    Friend WithEvents ProgressBar2 As System.Windows.Forms.ProgressBar
    Friend WithEvents lblStatus As System.Windows.Forms.Label
    Friend WithEvents btnCancel As System.Windows.Forms.Button

End Class



FrmZip.vb

Imports System.IO
Imports Ionic.Zip
Imports System.Threading
Imports System.ComponentModel

Public Class FrmZip
    
    Private _backgroundWorker1 As System.ComponentModel.BackgroundWorker
    Private _saveCanceled As Boolean
    Private _totalBytesAfterCompress As Long
    Private _totalBytesBeforeCompress As Long
    Private _nFilesCompleted As Integer
    Private _progress2MaxFactor As Integer
    Private _entriesToZip As Integer
    Private _appCuKey As Microsoft.Win32.RegistryKey
    'Private AppRegyPath As String = "Software\Ionic\VBzipUp"
    Private rvn_ZipFile As String = "zipfile"
    Private rvn_DirToZip As String = "dirToZip"

    ' Delegates for invocation of UI from other threads
    Private Delegate Sub SaveEntryProgress(ByVal e As SaveProgressEventArgs)
    Private Delegate Sub ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    
    Private Sub btnDirBrowse_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDirBrowse.Click
        Dim folderName As String = Me.tbDirToZip.Text
        Dim dlg1 As New FolderBrowserDialog

        dlg1.SelectedPath = IIf(Directory.Exists(folderName), folderName, "c:\")
        dlg1.ShowNewFolderButton = False
        If (dlg1.ShowDialog = DialogResult.OK) Then
            'Me._folderName = dlg1.get_SelectedPath
            Me.tbDirToZip.Text = folderName
        End If
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZipUp.Click
        Me.KickoffZipup()
    End Sub


    Private Sub KickoffZipup()
        Dim folderName As String = Me.tbDirToZip.Text
        If (((Not folderName Is Nothing) AndAlso (folderName <> "")) AndAlso ((Not Me.tbZipToCreate.Text Is Nothing) AndAlso (Me.tbZipToCreate.Text <> ""))) Then
            If File.Exists(Me.tbZipToCreate.Text) Then
                If (MessageBox.Show(String.Format("The file you have specified ({0}) already exists.  Do you want to overwrite this file?", _
                                                  Me.tbZipToCreate.Text), "Confirmation is Required", _
                                                  MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> DialogResult.Yes) Then
                    Return
                End If
                File.Delete(Me.tbZipToCreate.Text)
            End If
            Me._saveCanceled = False
            Me._nFilesCompleted = 0
            Me._totalBytesAfterCompress = 0
            Me._totalBytesBeforeCompress = 0
            Me.btnZipUp.Enabled = False
            Me.btnZipUp.Text = "Zipping..."
            Me.btnCancel.Enabled = True
            Me.lblStatus.Text = "Zipping..."
            Dim options As New WorkerOptions
            options.ZipName = Me.tbZipToCreate.Text
            options.Folder = folderName
            
            _backgroundWorker1 = New System.ComponentModel.BackgroundWorker()
            _backgroundWorker1.WorkerSupportsCancellation = False
            _backgroundWorker1.WorkerReportsProgress = False
            AddHandler Me._backgroundWorker1.DoWork, New DoWorkEventHandler(AddressOf Me.DoSave)
            _backgroundWorker1.RunWorkerAsync(options)
            
        End If
    End Sub

    
    Private Sub DoSave(ByVal sender As Object, ByVal e As DoWorkEventArgs)
        Dim options As WorkerOptions = e.Argument
        'Try
            Using zip1 As ZipFile = New ZipFile
                zip1.AddDirectory(options.Folder)
                Me._entriesToZip = zip1.EntryFileNames.Count
        '        Me.SetProgressBars()
                AddHandler zip1.SaveProgress, New EventHandler(Of SaveProgressEventArgs)(AddressOf Me.zip1_SaveProgress)
          zip1.Save(options.ZipName)
       End Using
        'Catch exc1 As Exception
        '    MessageBox.Show(String.Format("Exception while zipping: {0}", exc1.Message))
        '    Me.btnCancel_Click(Nothing, Nothing)
        'End Try
    End Sub

    
    Private Sub zip1_SaveProgress(ByVal sender As Object, ByVal e As SaveProgressEventArgs)
        If Me._saveCanceled Then
            e.Cancel = True
            Return
        End If

        Select Case e.EventType
            Case ZipProgressEventType.Saving_AfterWriteEntry
                'Me.StepArchiveProgress(e)
                Exit Select
            Case ZipProgressEventType.Saving_Completed
                Me.SaveCompleted()
                Exit Select
            Case ZipProgressEventType.Saving_EntryBytesRead
               ' Me.StepEntryProgress(e)
                Exit Select
        End Select
    End Sub



'    Private Sub StepArchiveProgress(ByVal e As SaveProgressEventArgs)
'        If Me.progressBar1.InvokeRequired Then
'            Me.progressBar1.Invoke(New SaveEntryProgress(AddressOf Me.StepArchiveProgress), New Object() {e})
'        ElseIf Not Me._saveCanceled Then
'            Me._nFilesCompleted += 1
'            Me.progressBar1.PerformStep()
'            Me._totalBytesAfterCompress = (Me._totalBytesAfterCompress + e.CurrentEntry.CompressedSize)
'            Me._totalBytesBeforeCompress = (Me._totalBytesBeforeCompress + e.CurrentEntry.UncompressedSize)
'            ' progressBar2 is the one dealing with the item being added to the archive
'            ' if we got this event, then the add of that item (or file) is complete, so we 
'            ' update the progressBar2 appropriately.
'            Me.progressBar2.Value = Me.progressBar2.Maximum = 1
'            MyBase.Update()
'        End If
'    End Sub


    Private Sub SaveCompleted()
        If Me.lblStatus.InvokeRequired Then
            Me.lblStatus.Invoke(New MethodInvoker(AddressOf SaveCompleted))
            'Me.lblStatus.Invoke(New MethodInvoker(Me, DirectCast(Me.SaveCompleted, IntPtr)))
        Else
            Me.lblStatus.Text = String.Format("Done, Compressed {0} files, {1:N0}% of original", Me._nFilesCompleted, ((100 * Me._totalBytesAfterCompress) / CDbl(Me._totalBytesBeforeCompress)))
            Me.ResetState()
        End If
    End Sub


'    Private Sub StepEntryProgress(ByVal e As SaveProgressEventArgs)
'        If Me.progressBar2.InvokeRequired Then
'            Me.progressBar2.Invoke(New SaveEntryProgress(AddressOf Me.StepEntryProgress), New Object() {e})
'        ElseIf Not Me._saveCanceled Then
'            If (Me.progressBar2.Maximum = 1) Then
'                Dim entryMax As Long = e.TotalBytesToTransfer
'                Dim absoluteMax As Long = &H7FFFFFFF
'                Me._progress2MaxFactor = 0
'                Do While (entryMax > absoluteMax)
'                    entryMax = (entryMax / 2)
'                    Me._progress2MaxFactor += 1
'                Loop
'                If (CInt(entryMax) < 0) Then
'                    entryMax = (entryMax * -1)
'                End If
'                Me.progressBar2.Maximum = CInt(entryMax)
'                Me.lblStatus.Text = String.Format("{0} of {1} files...({2})", (Me._nFilesCompleted + 1), Me._entriesToZip, e.CurrentEntry.FileName)
'            End If
'            Dim xferred As Integer = CInt((e.BytesTransferred >> Me._progress2MaxFactor))
'            Me.progressBar2.Value = IIf((xferred >= Me.progressBar2.Maximum), Me.progressBar2.Maximum, xferred)
'            MyBase.Update()
'        End If
'    End Sub


    Private Sub ResetState()
        Me.btnCancel.Enabled = False
        Me.btnZipUp.Enabled = True
        Me.btnZipUp.Text = "Zip it!"
        Me.progressBar1.Value = 0
        Me.progressBar2.Value = 0
        Me.Cursor = Cursors.Default
    End Sub



'    Private Sub SetProgressBars()
'        If Me.ProgressBar1.InvokeRequired Then
'            'Me.ProgressBar1.Invoke(New MethodInvoker(Me, DirectCast(Me.SetProgressBars, IntPtr)))
'            Me.ProgressBar1.Invoke(New MethodInvoker(AddressOf SetProgressBars))
'        Else
'            Me.ProgressBar1.Value = 0
'            Me.ProgressBar1.Maximum = Me._entriesToZip
'            Me.ProgressBar1.Minimum = 0
'            Me.ProgressBar1.Step = 1
'            Me.ProgressBar2.Value = 0
'            Me.ProgressBar2.Minimum = 0
'            Me.ProgressBar2.Maximum = 1
'            Me.ProgressBar2.Step = 2
'        End If
'    End Sub


'    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
'        If Me.lblStatus.InvokeRequired Then
'            Me.lblStatus.Invoke(New ButtonClick(AddressOf Me.btnCancel_Click), New Object() {sender, e})
'        Else
'            Me._saveCanceled = True
'            Me.lblStatus.Text = "Canceled..."
'            Me.ResetState()
'        End If
'    End Sub


'    Private Sub SaveFormToRegistry()
'        If AppCuKey IsNot Nothing Then
'            If Not String.IsNullOrEmpty(tbZipToCreate.Text) Then
'                AppCuKey.SetValue(rvn_ZipFile, Me.tbZipToCreate.Text)
'            End If
'            If Not String.IsNullOrEmpty(tbDirToZip.Text) Then
'                AppCuKey.SetValue(rvn_DirToZip, tbDirToZip.Text)
'            End If
'        End If
'    End Sub

'    Private Sub LoadFormFromRegistry()
'        If AppCuKey IsNot Nothing Then
'            Dim s As String
'            s = AppCuKey.GetValue(rvn_ZipFile)
'            If Not String.IsNullOrEmpty(s) Then
'                Me.tbZipToCreate.Text = s
'            End If
'            s = AppCuKey.GetValue(rvn_DirToZip)
'            If Not String.IsNullOrEmpty(s) Then
'                tbDirToZip.Text = s
'            End If
'        End If
'    End Sub


'    Public ReadOnly Property AppCuKey() As Microsoft.Win32.RegistryKey
'        Get
'            If (_appCuKey Is Nothing) Then
'                Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegyPath, True)
'                If (Me._appCuKey Is Nothing) Then
'                    Me._appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegyPath)
'                End If
'            End If
'            Return _appCuKey
'        End Get
'    End Property

'    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
'        SaveFormToRegistry()
'    End Sub

'    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'        LoadFormFromRegistry()
'    End Sub
End Class

Public Class WorkerOptions
    ' Fields
    'Public Comment As String
    'Public CompressionLevel As CompressionLevel
    'Public Encoding As String
    'Public Encryption As EncryptionAlgorithm
    Public Folder As String
    'Public Password As String
    'Public Zip64 As Zip64Option
    'Public ZipFlavor As Integer
    Public ZipName As String
End Class





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值