Working method for UnzipMBS

Anybody out there have a method that works for unzipping a file? Seen several examples on the MBS website. Cant get anything to work.

Which example have you tried? What didn’t work? Exception, crash, nothing happened? Have you written to Christian? And as usual: post the code you tried!

Code I use : I have taken out a lot of me-specific stuff, so hopefully it still runs, but it would be up to you to test it.

[code]
dim f as FolderItem
dim z as UnZipMBS

//dim lines(-1) as string
dim s as string

dim nameofzipfile as string = “myzip.zip”
dim info as UnZipFileInfoMBS
dim destfolder as folderitem
dim ff as folderitem
dim bSkip as boolean
destfolder = specialfolder.documents.child(“thefolder”)

if destfolder.exists then
else
  destfolder.CreateAsFolder
end if
if not destfolder.exists then
  msgbox "Support folder cannot be created in Documents"
end if

dim b as BinaryStream
f = somefolderitem.child(nameofzipfile) //this is the zip file
if f=nil or f.Exists=False then
MsgBox “Failed to find support file.”
exit sub
end if

z=new UnZipMBS(f)

dim filesofar as integer
dim totfiles as integer
totfiles = z.count
filesofar = -1

//reset series
z.GoToFirstFile
do

dim isFolder as Boolean=false
dim path as string = z.FileName

filesofar = filesofar + 1
// we need to know text encoding, so we guess ASCII here
path = DefineEncoding(path, encodings.ASCII)

if Right(path,1)="/" then
  isFolder=true
end if

f=getpath(destfolder, path)

info=z.FileInfo


bskip = false

if instr(z.FileName, "/.") > 1    then
  bskip = true
end if


if not bskip then

  if isfolder then
    
    f.CreateAsFolder
  else

    try
      f.Delete
    catch
    end try

    b =f.CreateBinaryFile("")
    
    if b<>nil then
      z.OpenCurrentFile
      if z.Lasterror=0 then
        
        do
          s=z.ReadCurrentFile(100000)
          b.Write s
        loop until lenb(s) = 0
        
        z.CloseCurrentFile
        b.Close
      end if
    else
      //lines.Append "Failed to create binary stream for "+f.AbsolutePath
    end if
  end if
  
  f.ModificationDate=info.Date
  f.CreationDate=info.date
end if
z.GoToNextFile

loop until z.Lasterror<>0
z.Close

try
z = nil
catch
end try

exception
msgbox “Error extracting files”[/code]

I haven’t tried this… but what happens if you “launch” a folderitem that points to a zip file??

Still working in a 2 years old project.

[code]// Unzip given zip-file
Dim f1 as Folderitem // make f1 = your zip-file
Dim f2 as Folderitem // make f2 = destination folder

Dim error As String
Dim unzipPRG As UnZipMBS

unzipPRG = New UnZipMBS(f1)
If Not unzipPRG.ExtractFiles(f2, error) Then msgbox(“problem”)
unzipPRG.Close[/code]

What doesn’t work exactly?

Also please note we have newer Archive classes which do more (e.g. tar):
http://www.monkeybreadsoftware.net/pluginpart-archive.shtml

[quote=389286:@Christian Schmitz]What doesn’t work exactly?

Also please note we have newer Archive classes which do more (e.g. tar):
http://www.monkeybreadsoftware.net/pluginpart-archive.shtml[/quote]

Probably less of it not working and more of me not hooking it up correctly. What it was doing was looping through and extracting the files out of the zip and not placing it into an app folder. Ended up with a lot of extracted files in the wrong folder.

[quote=389279:@Joost Rongen]Still working in a 2 years old project.

[code]// Unzip given zip-file
Dim f1 as Folderitem // make f1 = your zip-file
Dim f2 as Folderitem // make f2 = destination folder

Dim error As String
Dim unzipPRG As UnZipMBS

unzipPRG = New UnZipMBS(f1)
If Not unzipPRG.ExtractFiles(f2, error) Then msgbox(“problem”)
unzipPRG.Close[/code][/quote]

Set this up and it works great. The only problem is that I am left with a _MACOSX folder that shouldn’t be there. How do I prevent this from happening? Thanks a lot for the example code.

[quote=389279:@Joost Rongen]Still working in a 2 years old project.

[code]// Unzip given zip-file
Dim f1 as Folderitem // make f1 = your zip-file
Dim f2 as Folderitem // make f2 = destination folder

Dim error As String
Dim unzipPRG As UnZipMBS

unzipPRG = New UnZipMBS(f1)
If Not unzipPRG.ExtractFiles(f2, error) Then msgbox(“problem”)
unzipPRG.Close[/code][/quote]
This works but it will not open the app file that was extracted. When I open using finder, the app works. When extracting using this method and MBS, its not working.

The zip file holds an app?
I wonder if it is being recognised as an app bundle properly afterwards?
After unzipping this way and ctrl-clicking on the ‘app’ , does it offer to ‘show package contents’?

[quote=389374:@Jeff Tullin]The zip file holds an app?
I wonder if it is being recognised as an app bundle properly afterwards?
After unzipping this way and ctrl-clicking on the ‘app’ , does it offer to ‘show package contents’?[/quote]
Yes it contains an app inside the zip file. I can see the package contents. When I unzip using finder, and look at app folder size, it is the exact same as the app folder size that was unzipped using this method. Everything looks like it should work. When I try to open it, it just says the application cannot be opened.

[code]Private Sub Unzip(f1 as FolderItem, f2 as FolderItem)

Dim sError As String
Dim UnzipFile As UnZipMBS

UnzipFile = New UnZipMBS(f1)

If Not UnzipFile.ExtractFiles(f2, sError) Then MsgBox("Failed to unzip the file. ")

UnzipFile.Close

End Sub

[/code]

@Jeffery Lemons if you put it in a universal methode for all your projects, you should also check the folderitems before calling the unzip plugin.

I can recreate this.

I zipped up an app using Finder.
Called it mtest.zip and put it on the desktop.

If I unpack it using this code:

[code] dim f1 as folderitem
dim f2 as folderitem
f1 = SpecialFolder.desktop.child(“mtest.zip”)
f2 = SpecialFolder.desktop
Dim sError As String
Dim UnzipFile As UnZipMBS

UnzipFile = New UnZipMBS(f1)

If Not UnzipFile.ExtractFiles(f2, sError) Then
MsgBox("Failed to unzip the file. ")
end if

UnzipFile.Close[/code]

Then I get a file on the desktop that looks and feels like an app, it has an icon, is recognised as a bundle
However it will not start.
The reason is that the executable file inside MacOS , inside Contents, inside the Packaged app
is not marked as executable.

I got it to start by using Terminal:

[code]JeffTulComputer:~ jefftullin$ cd desktop
JeffTulComputer:desktop jefftullin$ cd myapp.app
JeffTulComputer:macstitch.app jefftullin$ cd contents
JeffTulComputer:contents jefftullin$ cd macos
JeffTulComputer:macos jefftullin$ chmod +x MyApp

[/code]
The problem IS a file permissions issue.

Double clicking the zip file in Finder produces an exectuable file, but unzipping using UnzipMBS does not.

The docs for UnzipMBS say ‘no Apple or Microsoft extensions for special file flags or permissions.

So knowing this you may need to issue a Shell command after unzipping to do what I did by hand above.
A bit like this (untested)

dim s as new shell s.Execute("chmod +x thefullnativepathtotheappname/Contents/MacOS/Appname")

Doesnt Xojo have a folderitem.permission property to do the chmod thing?

Why yes it does.
Not sure how I missed that.

https://documentation.xojo.com/index.php/FolderItem.Permissions

Read/write and executable:

myFile.Permissions = &o764

Much easier.

The only file I have a problem Zipping with MBS is an alias, so I have to ignore them.

[quote=389402:@Jeff Tullin]Why yes it does.
Not sure how I missed that.

https://documentation.xojo.com/index.php/FolderItem.Permissions

Read/write and executable:

myFile.Permissions = &o764

Much easier.[/quote]

setting the file permissions after I unzipped the file did not fix it. It will just gives me the same message saying the the application cannot be opened. Were you able to get it to work?

How was the ZIP created in the first place? Zipping a Mac app on Windows kills a bunch of required links and the app can’t be launched if it’s ever decompressed from the tar Xojo makes on Windows.

Is the error message any more specific than “Cannot be opened” ?

I created the zip file on my Mac. Then I uploaded it to my server. Downloaded it from the server using my updater app. Unzipped using updater app and the method above using MBS unzip. Nothing more specific on the messaging. Just “The application can’t be opened.”