Diff script for ZIP files

17 views
Skip to first unread message

Daniel Sahlberg

unread,
Sep 11, 2024, 4:10:51 AM9/11/24
to TortoiseSVN-dev
Hi,

I've got a usecase where I'm committing ZIP files to the repository. It is usefull for me to compare the contents of the ZIP file (filenames, date and time suffice, not contents of the individual files).

I've created a diff script, below, inspired by the existing diff scripts. Does the code make sense? Can we include it in the standard setup? If the response is positive, I'll commit it and do the necessary changes to the installation and default settings.

[[[
'
' TortoiseSVN Diff script for ZIP files
'
' Last commit by:
' $Author: $
' $Date: $
' $Rev: $
'
' Authors:
' Daniel Sahlberg, 2024
'

Set Arguments = WScript.Arguments
If Arguments.Count < 2 Then
MsgBox "Usage: [CScript | WScript] diff-zip.vbs base.zip new.zip", vbCritical, "Invalid arguments"
WScript.Quit 1
End If

Set ShellApplication = CreateObject("Shell.Application")
Set WScriptShell = CreateObject("WScript.Shell")
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")

Function ReadZip(pathToZipFile)
Set Folder = ShellApplication.NameSpace(pathToZipFile)
If Folder Is Nothing Then
MsgBox "Can't read " & pathToZipFile, vbCritical, "Invalid zip file"
Else
Set Items = Folder.Items
FileName = FileSystemObject.GetSpecialFolder(2) & "\" & FileSystemObject.GetTempName()
' 2 = ForWriting
' -1 = Opens the file as Unicode
Set File = FileSystemObject.OpenTextFile(FileName, 2, True, -1)
If File Is Nothing Then
MsgBox("Can't open " & File & " for writing")
Else
File.WriteLine(pathToZipFile)
File.WriteLine("")
MaxLength = 0
For i = 0 To Items.Count-1
If Len(Items.Item(i).Name) > MaxLength Then MaxLength = Len(Items.Item(i).Name)
Next
MaxLength = MaxLength+1
For i = 0 To Items.Count-1
Set Item = Items.Item(i)
File.WriteLine(Item.Name & String(MaxLength-Len(Item.Name), " ") & Item.ModifyDate & String(10-Len(Item.Size), " ") & Item.Size)
Next
File.WriteLine("")
File.WriteLine(Items.Count & " items")
File.Close()
ReadZip = FileName
End if
End if
End Function

BaseFile = ReadZip(Arguments(0))
NewFile = ReadZip(Arguments(1))

If BaseFile = "" Or NewFile = "" Then
WScript.Quit 1
End If

WScriptShell.Run """TortoiseMerge.exe"" /readonly /base:""" & BaseFile & """ /basename:""" & Arguments(0) & """ /mine:""" & NewFile & """ /minename:""" & Arguments(1) & """", 0, True

FileSystemObject.DeleteFile BaseFile
FileSystemObject.DeleteFile NewFile

WScript.Quit 0
]]]

Another option would be to have TortoiseMerge learn to read ZIP files and generating the directory listing internally, but it seems to be more work and I don't know if we have a library already that can read ZIP files or if we'd have to include additional dependencies.

Kind regards,
Daniel

Stefan

unread,
Sep 14, 2024, 2:19:28 AM9/14/24
to TortoiseSVN-dev
your script look very good. I'm all for it to include it in TSVN.
However, your script is in VBScript, I would prefer using JScript instead. Because VBScript is deprecated and will soon only be available as an optional Windows component:

As for a zip lib: we do have zlib linked in of course. But if we do support zip in TMerge, we also should support 7z/gzip/rar/..., otherwise users will keep requesting it ("you do support x, so please also do y").

Stefan

Daniel Sahlberg

unread,
Sep 14, 2024, 8:39:37 AM9/14/24
to TortoiseSVN-dev
lördag 14 september 2024 kl. 08:19:28 UTC+2 skrev Stefan:
your script look very good. I'm all for it to include it in TSVN.
However, your script is in VBScript, I would prefer using JScript instead. Because VBScript is deprecated and will soon only be available as an optional Windows component:

A good reason to practice some JavaScript skills. Script re-done and committed in r29709.

As for a zip lib: we do have zlib linked in of course. But if we do support zip in TMerge, we also should support 7z/gzip/rar/..., otherwise users will keep requesting it ("you do support x, so please also do y").

Which would also be nice improvements. But it is a lot more work so I think the diff-script solution is good enough.

I actually tried to make the diff-script work for 7z and tar files as well, since the File Explorer now support those. But Shell.Application.NameSpace didn't seem to want to open those files. I didn't dig any deeper though.

Kind regards,
Daniel

Reply all
Reply to author
Forward
0 new messages