How to compress a file containing parentheses in its filename?

How to compress a file containing parentheses (or other character interpreted by the Terminal) in its name with the Terminal zip command? (otherwise error message: “Unexpected end of zip file”)

Did you escape the parenthesis? For the shell you either need to use the \ in front like ( or you can try to quote the path like “my path with spaces”.

1 Like

The “Unexpected end of zip file” error typically occurs when you try to extract or open a corrupted or incomplete ZIP file. Some compression tools provide options to repair damaged ZIP files. The compression tool you’re using might not handle it well?

Thanks, I do that (works fine):

' Escaping parentheses ( )
Var escapedFilePath As String = ReplaceAll(sourcePath, "(", "\(")
escapedFilePath = ReplaceAll(escapedFilePath, ")", "\)")

'Escaping single quotes '
escapedFilePath = ReplaceAll(escapedFilePath, "'", "\'")

' Escaping double quotes "
escapedFilePath = ReplaceAll(escapedFilePath, """", "\""")

Wouldn’t it be safer to just replace all non-ASCII characters with a trailing backslash?
BTW: This may be possible using a single RegEx, maybe within a dedicated method?

Something like:

dim rx as new RegEx
rx.SearchPattern = "(?Umi-s)([\x00-\x2F|\x3A-\x40|\x91-\xFF]+)"
rx.ReplacementPattern = "\\$1"

dim rxOptions as RegExOptions = rx.Options
rxOptions.MatchEmpty = false
rxOptions.StringBeginIsLineBegin = false
rxOptions.StringEndIsLineEnd = false
rxOptions.LineEndType = 4
rxOptions.ReplaceAllMatches = true

dim replacedText as string = rx.Replace( sourceText )

The above replaces

This is an un-escaped Filename!.pdf


This\ is\ an\ un\-escaped\ Filename\!\.pdf

I’m sure @Kem_Tekinay knows an even better RegEx solution. :wink:

This simple conversion probably will fix all you got and some more:

// my insane file () ײ [] }{ -> ײmy insane file () \ײ [] }{ name.zipײ

Var enclosedPath As String = """"+ReplaceAll(sourcePath, """", "\""").ReplaceAll("\", "\\")+""""

Not bad @Rick_A :smiley:

Just for better readabillity:

// For those like me, who hate """" instead of Chr(34)... :-D
Var enclosedPath As String = Chr(34) + ReplaceAll(sourcePath, Chr(34), "\" + Chr(34)).ReplaceAll("\", "\\") + Chr(34)

Chr(24) vs """"

Function Call versus a Constant. For those like me, constants wins. For those who hates (I don’t know why) the 4 double quotation marks, and like to remember numeric codes, there’s the Unicode constant &u22

Var quote As String = &u22 + “quote” + &u22

1 Like

Just bc this looks so wrong :wink:

I think it depends on the eye of the beholder. I’m used to """" or "\"" (Basic or C variants)

For example, my future self will read the first one directly, the second one I must leave a remark:


1 Like