zip और unzip पर संपीड़न में निर्मित विंडो का उपयोग करके VBA विधियां भी हैं, जो सिस्टम को कैसे संचालित करती है, इस बारे में कुछ अंतर्दृष्टि प्रदान करनी चाहिए। आप इन तरीकों को अपनी पसंद की एक स्क्रिप्टिंग भाषा में बनाने में सक्षम हो सकते हैं।
मूलभूत सिद्धांत यह है कि विंडोज़ के भीतर आप एक ज़िप फ़ाइल को निर्देशिका के रूप में देख सकते हैं, और इसकी प्रतिलिपि बना सकते हैं। तो एक नई ज़िप फ़ाइल बनाने के लिए, आप बस .zip
एक्सटेंशन के साथ एक फ़ाइल बनाते हैं जिसमें खाली ज़िप फ़ाइल के लिए सही शीर्षलेख है। फिर आप इसे बंद करते हैं, और उन विंडो को बताते हैं जिन्हें आप फाइलों में कॉपी करना चाहते हैं जैसे कि यह एक और निर्देशिका थी।
अनजिपिंग आसान है - बस इसे एक निर्देशिका के रूप में देखें।
मामले में वेब पृष्ठों फिर से खो रहे हैं, यहाँ प्रासंगिक कोड के टुकड़े के कुछ कर रहे हैं:
ज़िप
Sub NewZip(sPath)
'Create empty Zip File
'Changed by keepITcool Dec-12-2005
If Len(Dir(sPath)) > 0 Then Kill sPath
Open sPath For Output As #1
Print #1, Chr$(80) & Chr$(75) & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
End Sub
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Rob Bovey
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function
Function Split97(sStr As Variant, sdelim As String) As Variant
'Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
Sub Zip_File_Or_Files()
Dim strDate As String, DefPath As String, sFName As String
Dim oApp As Object, iCtr As Long, I As Integer
Dim FName, vArr, FileNameZip
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
strDate = Format(Now, " dd-mmm-yy h-mm-ss")
FileNameZip = DefPath & "MyFilesZip " & strDate & ".zip"
'Browse to the file(s), use the Ctrl key to select more files
FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xl*), *.xl*", _
MultiSelect:=True, Title:="Select the files you want to zip")
If IsArray(FName) = False Then
'do nothing
Else
'Create empty Zip File
NewZip (FileNameZip)
Set oApp = CreateObject("Shell.Application")
I = 0
For iCtr = LBound(FName) To UBound(FName)
vArr = Split97(FName(iCtr), "\")
sFName = vArr(UBound(vArr))
If bIsBookOpen(sFName) Then
MsgBox "You can't zip a file that is open!" & vbLf & _
"Please close it and try again: " & FName(iCtr)
Else
'Copy the file to the compressed folder
I = I + 1
oApp.Namespace(FileNameZip).CopyHere FName(iCtr)
'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(FileNameZip).items.Count = I
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
End If
Next iCtr
MsgBox "You find the zipfile here: " & FileNameZip
End If
End Sub
अनज़िप
Sub Unzip1()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
If Fname = False Then
'Do nothing
Else
'Root folder for the new folder.
'You can also use DefPath = "C:\Users\Ron\test\"
DefPath = Application.DefaultFilePath
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If
'Create the folder name
strDate = Format(Now, " dd-mm-yy h-mm-ss")
FileNameFolder = DefPath & "MyUnzipFolder " & strDate & "\"
'Make the normal folder in DefPath
MkDir FileNameFolder
'Extract the files into the newly created folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items
'If you want to extract only one file you can use this:
'oApp.Namespace(FileNameFolder).CopyHere _
'oApp.Namespace(Fname).items.Item("test.txt")
MsgBox "You find the files here: " & FileNameFolder
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
Google पर उपलब्ध विभिन्न वीबीस्क्रिप्ट कार्यान्वयन हैं, उदा। "[विंडोज शैल (एक्सपी, विस्टा, 2003 और 2008) और वीबीस्क्रिप्ट का उपयोग करके ज़िप और अनजिप फ़ाइलें (http://www.naterice.com/blog/template_permalink.asp?id=64)"। मैंने इनका परीक्षण नहीं किया है, लेकिन यह संभव है कि वे केवल 'ज़िप' को संपीड़ित न करें। – samjudson
ज़िप और संपीड़न के बीच आप क्या भेद कर रहे हैं? – Cheeso
आप उन्हें बिना किसी संपीड़न के फ़ाइलों को 'ज़िप' कर सकते हैं, जैसे यूनिक्स में 'tar'। यह आपको फ़ाइलों को पैकेज के रूप में वितरित करने की अनुमति देता है, लेकिन डिस्क स्थान के आकार को कम नहीं करता है। – samjudson