How to pack a proper zip file with Basic

classic Classic list List threaded Threaded
5 messages Options
Heiko Tietze-2 Heiko Tietze-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to pack a proper zip file with Basic

Although it's a FAQ I cannot find an answer. I'm trying to create an extension with Basic. Everything works fine when I pack the stuff with "zip -r foo.oxt ." given that I'm in the right directory, otherwise the parent dirs are included. But ChDir() is not available anymore in Basic so I have to go the hard way.

With com.sun.star.packages.Package the zip is always corrupt and cannot be read, even what PackageFormat is set to false. Any idea how to solve this?

Sub addToZip(oZipPackage, path$, file$)
  Dim xInputStream
  Dim sDestName As String
  Dim oZipPackageStream, oZipPackageFolder
 
  xInputStream=createUNOService("com.sun.star.ucb.SimpleFileAccess").OpenFileRead(convertToURL(path$ & file$))
  oZipPackageStream=oZipPackage.createInstance()
  oZipPackageStream.setInputStream(xInputStream)
  oZipPackageFolder=oZipPackage.getByHierarchicalName("")
  oZipPackageFolder.insertByName(file$,oZipPackageStream)
  oZipPackage.commitChanges()
End Sub

...
    oZipPackage = createUnoService("com.sun.star.packages.Package")
    Dim aArg As New com.sun.star.beans.NamedValue
    aArg.Name = "PackageFormat"
    aArg.Value = True 'plain Zip format
    oZipPackage.initialize(Array(oFileDialog.Files(0),aArg))
    addToZip(oZipPackage,sTempPath,"/description.xml")
    addToZip(oZipPackage,sTempPath,"/config.xcu")
    addToZip(oZipPackage,sTempPath,"/META-INF/manifest.xml")
    addToZip(oZipPackage,sTempPath,"/palettes/" & sPaletteName & ".soc")

Full code is at http://pastebin.com/e1rt0rSM but for only 1 day.

TIA,
Heiko
--
Dr. Heiko Tietze
UX Designer
Tel. +49 (0)179/1268509


_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice

signature.asc (499 bytes) Download Attachment
Oliver Brinzing Oliver Brinzing
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pack a proper zip file with Basic

Hi Heiko,

following code works for me to create a new zip archive and add a file

Regards
Oliver

OPTION EXPLICIT

Sub Test()
       
        Dim oZipArchive as Variant
  Dim aArg As New com.sun.star.beans.NamedValue
        Dim SURL as String
        Dim sFile as String
       
        sURL = ConvertToURL("D:\TEMP\")
        oZipArchive = createUnoService("com.sun.star.packages.Package")

     aArg.Name = "PackageFormat"
     aArg.Value = True
     oZipArchive.initialize(Array(sURL & "test.zip", aArg))

        sFile = "test.txt"
        Call AddFiletoZipArchive(oZipArchive, sURL, sFile)

End Sub

Sub AddFiletoZipArchive(oZipArchive as Object, ByVal sUrl as String, ByVal sFile as String)

        On Local Error Goto ErrorHandler

        Dim oSimpleFileAccess as Object
        Dim oInputStream as Object
        Dim oStream as Object
        Dim oPosition as Object
        Dim mArgs(0) as Variant

        mArgs(0) = False

        oStream = oZipArchive.createInstanceWithArguments(mArgs())
        oSimpleFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
        oInputStream = oSimpleFileAccess.openFileRead(sURL & sFile)
        oStream.setInputStream(oInputStream)

        oPosition = oZipArchive.getByHierarchicalName("")
        oPosition.insertByName(sFile, oStream)
        oZipArchive.commitChanges()
        oInputStream.closeInput()

        Exit Sub
ErrorHandler:
        MsgBox Err() & " - " & Error() & " - Row: " & Erl()
End Sub

_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
Heiko Tietze-2 Heiko Tietze-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pack a proper zip file with Basic

Thanks a lot for your code snippet and the heads-up. It's almost the same code, and after double-checking the minor differences with no success the issue must have been caused by something else. That brought me to the slash in the filename. And eventually it works now like a charm.


On 03/19/2017 09:32 AM, Oliver Brinzing wrote:

> Hi Heiko,
>
> following code works for me to create a new zip archive and add a file
>
> Regards
> Oliver
>
> OPTION EXPLICIT
>
> Sub Test()
>    
>     Dim oZipArchive as Variant
>      Dim aArg As New com.sun.star.beans.NamedValue
>     Dim SURL as String
>     Dim sFile as String
>    
>     sURL = ConvertToURL("D:\TEMP\")
>     oZipArchive = createUnoService("com.sun.star.packages.Package")
>
>     aArg.Name = "PackageFormat"
>     aArg.Value = True
>     oZipArchive.initialize(Array(sURL & "test.zip", aArg))
>
>     sFile = "test.txt"
>     Call AddFiletoZipArchive(oZipArchive, sURL, sFile)
>
> End Sub
>
> Sub AddFiletoZipArchive(oZipArchive as Object, ByVal sUrl as String, ByVal sFile as String)
>
>     On Local Error Goto ErrorHandler
>
>     Dim oSimpleFileAccess as Object
>     Dim oInputStream as Object
>     Dim oStream as Object
>     Dim oPosition as Object
>     Dim mArgs(0) as Variant
>
>     mArgs(0) = False
>
>     oStream = oZipArchive.createInstanceWithArguments(mArgs())
>     oSimpleFileAccess = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
>     oInputStream = oSimpleFileAccess.openFileRead(sURL & sFile)
>     oStream.setInputStream(oInputStream)
>
>     oPosition = oZipArchive.getByHierarchicalName("")
>     oPosition.insertByName(sFile, oStream)
>     oZipArchive.commitChanges()
>     oInputStream.closeInput()
>
>     Exit Sub
> ErrorHandler:
>     MsgBox Err() & " - " & Error() & " - Row: " & Erl()
> End Sub
>
> _______________________________________________
> LibreOffice mailing list
> [hidden email]
> https://lists.freedesktop.org/mailman/listinfo/libreoffice
--
Dr. Heiko Tietze
UX Designer
Tel. +49 (0)179/1268509


_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice

signature.asc (499 bytes) Download Attachment
Christian Lohmaier-3 Christian Lohmaier-3
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pack a proper zip file with Basic

In reply to this post by Heiko Tietze-2
> Full code is at http://pastebin.com/e1rt0rSM but for only 1 day.

This is not how mailinglists work, and enough reason for me not to
look at the rest of the post at all.

What's the point?

ciao
Christian
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
Heiko Tietze-2 Heiko Tietze-2
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to pack a proper zip file with Basic

This issue has been solved and meanwhile I sent out another request
for the final review. Would be great to get your feedback on the
referenced ticket
https://bugs.documentfoundation.org/show_bug.cgi?id=106644.

2017-03-20 15:00 GMT+01:00 Christian Lohmaier <[hidden email]>:
>> Full code is at http://pastebin.com/e1rt0rSM but for only 1 day.
>
> This is not how mailinglists work, and enough reason for me not to
> look at the rest of the post at all.
>
> What's the point?
>
> ciao
> Christian
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
Loading...