PDA

View Full Version : سوال: لست فايل هاي موجود در فايل زيپ يا كب در يك ليست باكس



milad.biroonvand
سه شنبه 27 مرداد 1388, 11:45 صبح
سلام دوستان عزيز

چطور ميشه ليست فايل هاي موجود در يك فايل زيپ ، كپ يا رار را داخل يك ليست باكس با فشردن روي يك دكمه نشان داد ؟

milad.biroonvand
سه شنبه 27 مرداد 1388, 14:22 عصر
من فعلا از اين كد استفاده مي كنم ، كسي ساده ترش رو نداره :چشمک:



Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
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.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents ListView1 As System.Windows.Forms.ListView
Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.ListView1 = New System.Windows.Forms.ListView
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 0)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(448, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open a CAB File"
'
'ListView1
'
Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2})
Me.ListView1.Location = New System.Drawing.Point(0, 24)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(448, 264)
Me.ListView1.TabIndex = 1
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ColumnHeader1
'
Me.ColumnHeader1.Width = 282
'
'ColumnHeader2
'
Me.ColumnHeader2.Width = 150
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(448, 286)
Me.Controls.Add(Me.ListView1)
Me.Controls.Add(Me.Button1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScree n
Me.Text = "Dynamic's CAB File Lister"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OD As New OpenFileDialog
With OD
.Filter = "CAB files|*.CAB"
.RestoreDirectory = True
End With
If OD.ShowDialog = DialogResult.OK Then
ListView1.Columns(0).Text = OD.FileName
Dim cabinet As New CAB(OD.FileName)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Public Class CAB
#Region "API / DELEGATES"
<StructLayout(LayoutKind.Sequential)> _
Public Structure FILE_IN_CABINET_INFO
Public NameInCabinet As IntPtr
Public FileSize As Int32
Public Win32Error As Int32
Public DosDate As Int16
Public DosTime As Int16
Public DosAttribs As Int16
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public FullTargetName As String
End Structure
Public Delegate Function PSP_FILE_CALLBACK_A(ByVal Context As Int32, ByVal Notification As Int32, ByVal Param1 As IntPtr, ByVal Param2 As IntPtr) As Int32
Private Declare Function SetupIterateCabinet Lib "setupapi.dll" Alias "SetupIterateCabinetA" (ByVal CabinetFile As String, ByVal Reserved As Int32, ByVal MsgHandler As PSP_FILE_CALLBACK_A, ByVal Context As Int32) As Int32
Private Declare Function DosDateTimeToFileTime Lib "kernel32.dll" (ByVal wFatDate As Int16, ByVal wFatTime As Int16, ByRef lpFileTime As FILETIME) As Int32
<StructLayout(LayoutKind.Sequential)> _
Private Structure FILETIME
'/// should be a Low & High part ( 2 x Int32 ) but 1 x Int64 works perfect with Date.FromFileTime
Public dwLowDateTime As Int64
End Structure
Private Const SPFILENOTIFY_CABINETINFO As Int32 = &H10
Private Const SPFILENOTIFY_FILEEXTRACTED As Int32 = &H13
Private Const SPFILENOTIFY_FILEINCABINET As Int32 = &H11
Private Const SPFILENOTIFY_NEEDNEWCABINET As Int32 = &H12
Private Const FILEOP_SKIP As Int32 = 2
#End Region
'/// HERE i open a CAB file to read it's contents...
Public Sub New(ByVal cabpath As String)
Dim cb As New PSP_FILE_CALLBACK_A(AddressOf PSP_FILE_CALLBACK)
SetupIterateCabinet(cabpath, 0, cb, 0)
End Sub
Private Function PSP_FILE_CALLBACK(ByVal Context As Int32, ByVal Notification As Int32, ByVal Param1 As IntPtr, ByVal Param2 As IntPtr) As Int32
'/// from msdn's C++ description @ http://msdn.microsoft.com/library/default.asp?url=/library/en-us/setupapi/setup/psp_file_callback.asp
'/// my interpretation...
Dim FILEINCABINET As Int32 = 0
Select Case Notification
Case SPFILENOTIFY_FILEINCABINET
'/// we've found a file in the CAB :)
FILEINCABINET = PSP_FILEFOUND_CALLBACK(Context, Notification, Param1, Param2)
'/// here we can also extract the files from the CAB.
'/// but we'll call PSP_FILEFOUND_CALLBACK for now.
End Select
Return FILEINCABINET
End Function
Private Function PSP_FILEFOUND_CALLBACK(ByVal Context As Int32, ByVal Notification As Int32, ByVal Param1 As IntPtr, ByVal Param2 As IntPtr) As Int32
Dim FILEFOUND As Int32
Select Case Context
Case 0
Dim f_in_cab As FILE_IN_CABINET_INFO = DirectCast(Marshal.PtrToStructure(Param1, GetType(FILE_IN_CABINET_INFO)), FILE_IN_CABINET_INFO)
Dim frm As Form1 = DirectCast(Form.ActiveForm, Form1)
'/// convert the IntPtr value of the filename to a readable string & add to a ListView...
Dim lvi As New ListViewItem(Marshal.PtrToStringAnsi(f_in_cab.Name InCabinet))
'/// the FILETIME should be 2 Int32's, but to get the resulting long value required i made it one Int64 :)
Dim ft As FILETIME
DosDateTimeToFileTime(f_in_cab.DosDate, f_in_cab.DosTime, ft)
Dim d As Date = Date.FromFileTime(ft.dwLowDateTime)
lvi.SubItems.Add(New ListViewItem.ListViewSubItem(lvi, d.ToLongDateString & " " & d.ToLongTimeString))
frm.ListView1.Items.Add(lvi)
FILEFOUND = FILEOP_SKIP
End Select
'/// must Return FILEFOUND to continue enumerating the files in the cab.
Return FILEFOUND
End Function
End Class