PDA

View Full Version : سوال: Unzip یک فایل داخل برنامه



Rain_Saeid
سه شنبه 09 دی 1393, 23:47 عصر
سلام و درود
دوستان عزیز بنده میخوام داخل برنامه یک فایل رو مثلا از متن text_1.text که آدرس داره باز کنم و در درایو C زخیره کنم
ممنون میشم راهنمایی بفرمایید با نمونه مثال
تشکر

Rain_Saeid
سه شنبه 09 دی 1393, 23:55 عصر
البته اینم بگم که از راههای زیادی استفاده کردم، ولی هرکدوم مشکلی داشتن
یا اینکه با Vb2010 بنده سازگار نبودن

gilsoft
چهارشنبه 10 دی 1393, 07:24 صبح
البته اینم بگم که از راههای زیادی استفاده کردم، ولی هرکدوم مشکلی داشتن
یا اینکه با Vb2010 بنده سازگار نبودن

سلام دوست عزیز

اول باید رفرنس Shell32 رو به پروژه‌ات اضافه کنی

من از کدهای زیر برای Zip / UnZip استفاده می‌کنم و مشکلی هم ندارم ... ( البته اینو هم بگم که تغییراتی تو کدهای اولیه دادم :چشمک: )




#Region " - Shell32 Zip/UnZip File(s) - "


Private Sub btnZip_Click(sender As System.Object, e As System.EventArgs) Handles btnZip.Click
Dim destPath As String = "D:\UnZiped"
Dim zipFileName As String = "D:\Ziped\1392.zip"
Zip(zipFileName, destPath)
End Sub


Private Sub btnUnZip_Click(sender As System.Object, e As System.EventArgs) Handles btnUnZip.Click
Dim destPath As String = "D:\UnZiped"
Dim zipFilePath As String = "D:\UnZiped" & "\1392.zip"
UnZip(destPath, zipFilePath)
End Sub


Sub Zip(zipFile As String, neededFiles As Object)
Me.Cursor = Cursors.WaitCursor
'1. Lets create an empty Zip File.
'The following data represents an empty zip file.
'Data for an empty zip file.
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}


'We have successfully made the empty zip file.
FileIO.FileSystem.WriteAllBytes(zipFile, startBuffer, False)


'2. Use the Shell32 to zip your files.
'Declare new shell class
Dim sc As New Shell32.Shell()


'Declare the folder which contains the files you want to zip.
Dim FromFiles As Shell32.Folder = sc.NameSpace(neededFiles)


'Declare your created empty zip file as folder .
Dim ToZip As Shell32.Folder = sc.NameSpace(zipFile)


'Copy the files into the empty zip file using the CopyHere command.
ToZip.CopyHere(FromFiles.Items, 132)
GC.Collect()
Me.Cursor = Cursors.Default
End Sub


Sub UnZip(destFolder As String, zipFile As String)
Me.Cursor = Cursors.WaitCursor
'Declare new shell class
Dim sc As New Shell32.Shell()


'Create directory in which you will unzip your files.
If IO.Directory.Exists(destFolder) = False Then IO.Directory.CreateDirectory(destFolder)


'Declare the folder where the files will be extracted.
Dim ToExtract As Shell32.Folder = sc.NameSpace(destFolder)


'Declare your input zip file as folder.
Dim FromZip As Shell32.Folder = sc.NameSpace(zipFile)


'Extract the files from the zip file using the CopyHere command.
ToExtract.CopyHere(FromZip.Items, 4)
GC.Collect()
Me.Cursor = Cursors.Default
End Sub


#End Region ' - Shell32 Zip/UnZip File(s) -


موفق باشید .....

Rain_Saeid
شنبه 13 دی 1393, 14:11 عصر
سلام دوست عزیز

اول باید رفرنس Shell32 رو به پروژه‌ات اضافه کنی

من از کدهای زیر برای Zip / UnZip استفاده می‌کنم و مشکلی هم ندارم ... ( البته اینو هم بگم که تغییراتی تو کدهای اولیه دادم :چشمک: )




#Region " - Shell32 Zip/UnZip File(s) - "


Private Sub btnZip_Click(sender As System.Object, e As System.EventArgs) Handles btnZip.Click
Dim destPath As String = "D:\UnZiped"
Dim zipFileName As String = "D:\Ziped\1392.zip"
Zip(zipFileName, destPath)
End Sub


Private Sub btnUnZip_Click(sender As System.Object, e As System.EventArgs) Handles btnUnZip.Click
Dim destPath As String = "D:\UnZiped"
Dim zipFilePath As String = "D:\UnZiped" & "\1392.zip"
UnZip(destPath, zipFilePath)
End Sub


Sub Zip(zipFile As String, neededFiles As Object)
Me.Cursor = Cursors.WaitCursor
'1. Lets create an empty Zip File.
'The following data represents an empty zip file.
'Data for an empty zip file.
Dim startBuffer() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}


'We have successfully made the empty zip file.
FileIO.FileSystem.WriteAllBytes(zipFile, startBuffer, False)


'2. Use the Shell32 to zip your files.
'Declare new shell class
Dim sc As New Shell32.Shell()


'Declare the folder which contains the files you want to zip.
Dim FromFiles As Shell32.Folder = sc.NameSpace(neededFiles)


'Declare your created empty zip file as folder .
Dim ToZip As Shell32.Folder = sc.NameSpace(zipFile)


'Copy the files into the empty zip file using the CopyHere command.
ToZip.CopyHere(FromFiles.Items, 132)
GC.Collect()
Me.Cursor = Cursors.Default
End Sub


Sub UnZip(destFolder As String, zipFile As String)
Me.Cursor = Cursors.WaitCursor
'Declare new shell class
Dim sc As New Shell32.Shell()


'Create directory in which you will unzip your files.
If IO.Directory.Exists(destFolder) = False Then IO.Directory.CreateDirectory(destFolder)


'Declare the folder where the files will be extracted.
Dim ToExtract As Shell32.Folder = sc.NameSpace(destFolder)


'Declare your input zip file as folder.
Dim FromZip As Shell32.Folder = sc.NameSpace(zipFile)


'Extract the files from the zip file using the CopyHere command.
ToExtract.CopyHere(FromZip.Items, 4)
GC.Collect()
Me.Cursor = Cursors.Default
End Sub


#End Region ' - Shell32 Zip/UnZip File(s) -


موفق باشید .....

با تشکر
ولی متاسفانه ارور میده
میشه همین رو بدون مشکل برام آپ کنید ؟
ممنون میشم

gilsoft
شنبه 13 دی 1393, 18:48 عصر
با تشکر
ولی متاسفانه ارور میده
میشه همین رو بدون مشکل برام آپ کنید ؟
ممنون میشم

سلام دوست عزیز

نباید Error بده ! .. چون از داخل نرم‌افزار خودم برات کپی کردم ( یعنی خودم دارم از همین کدها استفاده می‌کنم )

شما بفرما که:
رفرنس Shell32 رو به پروژه‌ات اضافه کردی ؟ ( یادآوری : دوتا Button .. یکی بنام btnZip و دیگری بنام btnUnZip هم باید به Form اضافه کنی )



127210

باز هم اگه مشکلی بود .. من در خدمتم .....

موفق باشید .....

erfan_urchin
شنبه 13 دی 1393, 22:12 عصر
سلام و درود
دوستان عزیز بنده میخوام داخل برنامه یک فایل رو مثلا از متن text_1.text که آدرس داره باز کنم و در درایو C زخیره کنم
ممنون میشم راهنمایی بفرمایید با نمونه مثال
تشکر
شما اول برو Add Reference و گزینه System.IO.Compression.FileSystem رو ادد کن
بعد بیا به پروژت System.IO.Compression رو ایمپورت کن و بعد از کد زیر استفاده کن
ZipFile.ExtractToDirectory("C:\MyFile.zip", "D:\")

Rain_Saeid
یک شنبه 14 دی 1393, 23:07 عصر
با تشکر از شما
ولی بازم ارور داد دوست عزیز
عکسشو ضمیمه کردم :

gilsoft
دوشنبه 15 دی 1393, 21:03 عصر
با تشکر از شما
ولی بازم ارور داد دوست عزیز
عکسشو ضمیمه کردم :

سلام دوست عزیز

احتمالا Framework پروژه‌ی شما ، ورژن‌ش پایینه !! ... من از ورژن Framework 4.0 استفاده می‌کنم ....

موفق باشید ....