هل يمكن كتابة ضغط ZIP المدمج في Windows؟
-
09-06-2019 - |
سؤال
هل يمكن كتابة ضغط ZIP المضمن في نظام التشغيل Windows XP/Vista/2003/2008 على الإطلاق؟ما هو الملف القابل للتنفيذ الذي يجب علي استدعاؤه من ملف BAT/CMD؟أم أنه من الممكن أن تفعل ذلك مع VBScript؟
وأنا أدرك أن هذا ممكن باستخدام برنامج لضغط الملفات, 7-الرمز البريدي والتطبيقات الخارجية الأخرى، لكني أبحث عن شيء لا يتطلب تثبيت أي تطبيقات خارجية.
المحلول
هناك طرق VBA ل أَزِيز و فك الضغط باستخدام النوافذ المضمنة بالضغط أيضًا، والتي من شأنها أن تعطي فكرة عن كيفية عمل النظام.قد تتمكن من إنشاء هذه الأساليب في لغة برمجة نصية من اختيارك.
المبدأ الأساسي هو أنه داخل نظام التشغيل Windows يمكنك التعامل مع ملف مضغوط كدليل، والنسخ منه وإليه.لذا، لإنشاء ملف مضغوط جديد، ما عليك سوى إنشاء ملف بامتداد .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
نصائح أخرى
نعم، يمكن كتابتها باستخدام VBScript.على سبيل المثال، يمكن للكود التالي إنشاء ملف مضغوط من دليل:
Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")
MyTarget = Wscript.Arguments.Item(0)
MySource = Wscript.Arguments.Item(1)
Wscript.Echo "Adding " & MySource & " to " & MyTarget
'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close
winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items
do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
wscript.sleep 1000
loop
Set winShell = Nothing
Set fso = Nothing
قد تجد أيضا http://www.naterice.com/blog/template_permalink.asp?id=64 مفيد لأنه يتضمن تطبيق Unzip/Zip الكامل في VBScript.
إذا قمت بفحص الحجم كل 500 مللي ثانية بدلاً من عدد العناصر، فسيعمل ذلك بشكل أفضل مع الملفات الكبيرة.يقوم Win 7 بكتابة الملف على الفور على الرغم من عدم الانتهاء من ضغطه:
set fso=createobject("scripting.filesystemobject")
Set h=fso.getFile(DestZip)
do
wscript.sleep 500
max = h.size
loop while h.size > max
يعمل بشكل رائع مع كميات هائلة من ملفات السجل.
فقط للتوضيح:GZip ليست خوارزمية MS فقط كما اقترح جاي ستاربوك في تعليقه في أغسطس.يستخدم GZipStream في System.IO.Compression خوارزمية Deflate، تمامًا مثل مكتبة zlib والعديد من أدوات الضغط الأخرى.هذه الفئة قابلة للتشغيل المتبادل بشكل كامل مع أدوات يونكس المساعدة مثل gzip.
فئة GZipStream غير قابلة للبرمجة من سطر الأوامر أو VBScript لإنتاج ملفات ZIP، لذا فهي وحدها لن تكون إجابة لطلب الناشر الأصلي.
الحر دوت نت زيب تقوم المكتبة بقراءة وإنشاء ملفات مضغوطة، ويمكن كتابتها من VBScript أو Powershell.ويتضمن أيضًا أدوات سطر الأوامر لإنتاج وقراءة/استخراج الملفات المضغوطة.
إليك بعض التعليمات البرمجية لـ VBScript:
dim filename
filename = "C:\temp\ZipFile-created-from-VBScript.zip"
WScript.echo("Instantiating a ZipFile object...")
dim zip
set zip = CreateObject("Ionic.Zip.ZipFile")
WScript.echo("using AES256 encryption...")
zip.Encryption = 3
WScript.echo("setting the password...")
zip.Password = "Very.Secret.Password!"
WScript.echo("adding a selection of files...")
zip.AddSelectedFiles("*.js")
zip.AddSelectedFiles("*.vbs")
WScript.echo("setting the save name...")
zip.Name = filename
WScript.echo("Saving...")
zip.Save()
WScript.echo("Disposing...")
zip.Dispose()
WScript.echo("Done.")
إليك بعض التعليمات البرمجية لـ Powershell:
[System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");
$directoryToZip = "c:\\temp";
$zipfile = new-object Ionic.Zip.ZipFile;
$e= $zipfile.AddEntry("Readme.txt", "This is a zipfile created from within powershell.")
$e= $zipfile.AddDirectory($directoryToZip, "home")
$zipfile.Save("ZipFiles.ps1.out.zip");
في ملف .bat أو .cmd، يمكنك استخدام أدوات zipit.exe أو unzip.exe.على سبيل المثال:
zipit NewZip.zip -s "This is string content for an entry" Readme.txt src
إليك محاولتي لتلخيص نوافذ القدرات المضمنة للضغط وإلغاء الضغط - كيف يمكنني ضغط (/ zip ) وفك ضغط (/ unzip ) الملفات والمجلدات بملف دفعي دون استخدام أي أدوات خارجية؟
مع بعض الحلول المقدمة التي يجب أن تعمل على كل جهاز يعمل بنظام Windows تقريبًا.
فيما يتعلق ب shell.application وWSH فضلت jscriptلأنه يسمح بملف دفعي/jscript مختلط (بامتداد .bat) لا يتطلب ملفات مؤقتة. لقد قمت بوضع إمكانيات فك الضغط والضغط في ملف واحد بالإضافة إلى بعض الميزات الإضافية.
يوجد كل من الملفات التنفيذية المضغوطة وفك الضغط (بالإضافة إلى مجموعة كبيرة من التطبيقات المفيدة الأخرى) في حزمة UnxUtils المتوفرة على SourceForge (http://sourceforge.net/projects/unxutils).انسخها إلى موقع في المسار الخاص بك، مثل "c:\windows"، وستتمكن من تضمينها في البرامج النصية الخاصة بك.
هذا ليس الحل الأمثل (أو الحل الذي طلبته) ولكنه عمل لائق.
لإنشاء أرشيف مضغوط، يمكنك استخدام الأداة المساعدة MAKECAB.EXE