# zipping a file by command line

**URL:** <https://forum.xojo.com/t/zipping-a-file-by-command-line/48174>\
**Category:** macOS\
**Created:** [April 16, 2019, 6:11am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174 "2019-04-16T06:11:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [April 16, 2019, 6:11am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/1 "2019-04-16T06:11:26Z")

</div>

Hello,  
I use this code to zip a file:

`dim dlg as new OpenDialog
dlg.InitialDirectory = SpecialFolder.Desktop
dim f as FolderItem = dlg.ShowModalWithin(self)
if f <> nil then
  dim h as FolderItem = SpecialFolder.Desktop.Child("myZip")
 // if h = nil and not h.Exists then
    //h.CreateAsFolder
  //end if
  dim s as new Shell
  s.Execute "zip " + h.ShellPath + " " + f.ShellPath
end if`

The file gets zipped all right, but when I unzip it by double-click I find it nested in two folders:

1. Users, containing the following folder
2. CarloRubini, containing the actual file

Is there a way to zip the file in such a way that the two folders get skipped?

Thank you.

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [April 16, 2019, 6:44am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/2 "2019-04-16T06:44:43Z")

</div>

My guess from that is that you need to cd into the folder above “f” before creating the zip file. When yo upass h.ShellPath, you’re passing the complete path, so:

```auto
/Users/CarloRubini/TheStuffYouWanted
```

Therefore, you need to cd into /Users/CarloRubini/ and then execute the ZIP command:

```auto
s.Execute "cd " + f.Parent.NativePath + " ; zip " + h.ShellPath + " " + f.Name
```

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 9:33am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/3 "2019-04-16T09:33:59Z")

</div>

I have a similar problem. In my program I create a temporary folder with all files/folders generated by my program. Let’s say the folder is stored in SpecialFolder.Desktop.

I want to let my users know where the zip file is created. I would like to offer him an OpenDialog where he can select the file path and its name. Now, unnoticed by the user, a zip file should be output from the temporary folder exactly at this point. After this has happened successfully (you can check this via the shell object), the temporary folder should be deleted on the desktop.

The above shell call to create zip files should also work on Linux, right? Is there a Windows counterpart?

Previously, Apple’s website gave a detailed description of the ZIP command, this page has not existed for a long time and one can only hope that the shell command is not deprecated!

Any suggestions or hints?

---

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [April 16, 2019, 9:54am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/4 "2019-04-16T09:54:04Z")

</div>

Hi Tim,  
it works all right. Yet only if the name of the file selected to be zipped does not contain any space.  
I unsuccesfuly tried encodeUrlComponent.  
Any idea?

Thank you.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 16, 2019, 10:01am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/5 "2019-04-16T10:01:51Z")

</div>

Put the name inside quotes (the one you pass to the shell) just like in the Terminal).

Edit (explanation):  
You have a Windows 10 folder in your user directory.

Fire the Terminal,  
type: `ls Windows 10 -l`

you get an error.

type: `ls "Windows 10" -l`  
you get the list of ityems in the passed folder name.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 16, 2019, 10:30am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/6 "2019-04-16T10:30:52Z")

</div>

> [@432833:@Martin Trippensee](#):
>
> Any suggestions or hints?

Use, in the Terminal, `man zip` and read

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 10:58am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/7 "2019-04-16T10:58:22Z")

</div>

I’ve created a small sample project. It creates the temporary folder which should be zipped on a user selected place. But it did not create a zip file.

[Zip file using macOS Terminal - Xojo Project](https://www.dropbox.com/s/dpc5081xb7v3aql/Zip%20Files.xojo_binary_project?dl=0)

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [April 16, 2019, 11:16am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/8 "2019-04-16T11:16:28Z")

</div>

@Martin Trippensee: what do you expect after an error? “Zip warning: name not matched Ohne Titel, zip error: nothing to do!”

---

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [April 16, 2019, 11:20am UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/9 "2019-04-16T11:20:17Z")

</div>

Here is a shell that, at present, works, ignoring the parent folders. It seems that -j does the trick:  
s.Execute "zip -j " etc.

As for the output name, I noticed that I have to leave out the extension part of the fileName; or add .zip to f.displayName

`dim dlg as new OpenDialog
dlg.InitialDirectory = SpecialFolder.Desktop
dim f as FolderItem = dlg.ShowModalWithin(self)
if f <> nil then
  dim r() as String = f.DisplayNameSplit(".")
  dim h as FolderItem = SpecialFolder.Desktop.Child(r(0))//or f.displayName + ".zip"
  dim s as new Shell
  s.Execute "zip -j " + h.ShellPath + " " + f.ShellPath
end if`

Thanks again.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 16, 2019, 12:10pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/10 "2019-04-16T12:10:03Z")

</div>

> [@432841:@Carlo Rubini](#):
>
> displayName

Today (AFAIK) DisplayName and Name behave the same, but this may change. You better use Name instead.

I will try that once back home.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 16, 2019, 1:10pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/11 "2019-04-16T13:10:11Z")

</div>

> [@432841:@Carlo Rubini](#):
>
> extension

The man zip page states for file extension:

Edit (remove useless spaces):

`If the name of the zip archive does not contain an extension, the extension .zip is added. If the
name already contains an extension other than .zip, the existing extension is kept unchanged. How-
ever, split archives (archives split over multiple files) require the .zip extension on the last
split.`

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 1:34pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/12 "2019-04-16T13:34:53Z")

</div>

> [@432840:@Beatrix Willius](#):
>
> what do you expect after an error? “Zip warning: name not matched Ohne Titel, zip error: nothing to do!”

I did a step forward.

[code]Dim input As FolderItem = TempFolder  
Dim output As FolderItem = SpecialFolder.Picture.Child(output.zip)  
Dim s As String

s = cd  + input.ShellPath + ; +\_  
zip -r  + output.ShellPath +   + input.ShellPath +  \*  
Shell1.Execute(s)[/code]  
This now packs my temporary folder including all files/folders into a user selected file path. But it stores a third folder in it Users/…. Please test it. I dont want the third folder. Can someone help please?

---

<div class="post-metadata">

**Author:** ![Carlo\_Rubini](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/94ad74/32.png) [@Carlo\_Rubini](https://forum.xojo.com/u/Carlo_Rubini)\
**Post date:** [April 16, 2019, 1:50pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/13 "2019-04-16T13:50:23Z")

</div>

@Emile Schwarz  
I read the zip page, but in practice I had to resort to the above code.

@Martin Trippensee

> [@](#):
>
> But it stores a third folder in it Users/….

May be the -j parameter may help you as it helped me (see above).

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 2:02pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/14 "2019-04-16T14:02:05Z")

</div>

> [@432848:@Carlo Rubini](#):
>
> May be the -j parameter may help you as it helped me (see above).

Sadly not. Adding -j after -r wont create anything and ill also not get any Shell.Result message.

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [April 16, 2019, 2:21pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/15 "2019-04-16T14:21:35Z")

</div>

Surround the names with double quotes. Using Martin’s code above:

`s = "cd """ + input.ShellPath + """ ; " +_
    "zip -r """ + output.ShellPath + """ """ + input.ShellPath + """ *"
Shell1.Execute(s)
`

Also, it’s good etiquette for the forums if you disable “Use smart quotes and dashes” in the Mac’s “Preferences” -\> “Keyboard” -\> “Text” settings 😃

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 3:07pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/16 "2019-04-16T15:07:51Z")

</div>

@Tim Jones thats totally weird. Using your String do also include the Users/… folders on my Mac. I modified the string to

`s = "cd """ + input.ShellPath + """ ; " +_
    "zip -r """ + output.ShellPath + """" + " " + """ + input.ShellPath + "" *"`  
Now it only writes to files I wanted, but if the output filename contains whitespaces, ill get for example Ohne\ Titel.zip instead of Ohne Titel.zip. Why?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [April 16, 2019, 3:21pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/17 "2019-04-16T15:21:14Z")

</div>

Because inside of quotes you don’t need to do the shell encoding I would guess.

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [April 16, 2019, 3:54pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/18 "2019-04-16T15:54:46Z")

</div>

How should I handle this clue? You suggest using NativePath instead?

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [April 16, 2019, 3:59pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/19 "2019-04-16T15:59:15Z")

</div>

Certainly.

---

<div class="post-metadata">

**Author:** ![Tim\_Jones](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_jones/32/8627_2.png) [@Tim\_Jones](https://forum.xojo.com/u/Tim_Jones)\
**Post date:** [April 16, 2019, 4:36pm UTC](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174/20 "2019-04-16T16:36:32Z")

</div>

Apologies - I realize that I didn’t change that in my code examples. Trixie is correct.

You should always use “.NativePath” and quotes for the best results on all three platforms when dealing with passing file paths in a shell.

[Next page](https://forum.xojo.com/t/zipping-a-file-by-command-line/48174.md?page=2)
