Results 1 to 8 of 8

Thread: Read / write directly to zip

  1. #1

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    Question Read / write directly to zip

    Hello,

    as many of you already knows I'm creating an extension for Excel. The new Userform control called Virtual Forms. It is created in vb6.

    With help of the big names in the vb6 world (I will not mention by name, to not miss someone) the control will work in 32-bit and 64-bit Office.

    My new challenge is:
    The control needs one file where it stores it's data, for now this file is in the same folder where the workbook is (with extension *.vf)
    It gives me headaches, the workbook first needs to be save to get the folder, when redistibuting the workbook they also need to not forget this .vf file,.......

    Now I would like to embed this file in the workbook file.
    From 2007+ the office is using new file formats (*.xlsx, *.xlsm, *.docx,.....)
    They are in fact a zip files that contains xml files.

    They are called Office Open XML.

    What I would like is to embed the file where my control stores it's data into this zip files (if the standards are requesting I would also need to convert them to xml files) (for vb6 uses of this control I will leave the external .vf file)

    But what is bothering me is how to read and write to this file if it is embeded into this zip file (*.xlsx). Do I really need to extract and compress every time I need it.
    I think that in .net they are accessing it with memorystreams. But I'm not 100% sure.

    Does anyone have any idea?

    (you can try to rename your Book1.xlsx to Book1.zip and you can open it and see the content)

    Davor
    My projects:
    Virtual Forms
    VBA Telemetry

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,733

    Re: Read / write directly to zip

    In the CodeBank are multiple threads dealing with ZIPPING.
    Either using the builtin Windows functionality or by using an external DLL like ZLibWapi

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,909

    Re: Read / write directly to zip

    Davor,

    I've never had a need to Zip (although I do have some code examples, but I'd also rather direct you to the codebank). However, regarding Unzipping, I've used wqweto's cZip.cls module for some time, and it's always worked flawlessly for me.

    Also, if you have something like 7zip installed, you don't need to rename those .DOCX, .XLSX, etc. files. Just pull up the context menu (i.e., right-click), and open them with 7zip.

    Good luck with re-Zipping. If you get some bullet-proof code worked out, be sure to post your results.

    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Read / write directly to zip

    Just as a note, while office docs are actually zip files, trying to use them with IStorage (StgOpenStorage, etc) inexplicably goes by filename extension. So it fails with file.xlsm, but if you rename it to file.zip, and no other changes whatsoever, it then will work.

  5. #5
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Read / write directly to zip

    Quote Originally Posted by fafalone View Post
    Just as a note, while office docs are actually zip files, trying to use them with IStorage (StgOpenStorage, etc) inexplicably goes by filename extension. So it fails with file.xlsm, but if you rename it to file.zip, and no other changes whatsoever, it then will work.
    I seem to remember this was done by a security patch added to support Office security. Malware was using IStorage access to inject macro viruses into Office documents written in the newer XML formats with ease. I doubt there is any easy way to get around this diversionary behavior.

    I'm trying to find the Office Blog post about it but so far nothing has turned up.

  6. #6
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: Read / write directly to zip

    There is an easy way to get around it:
    if extension = .xlsm or etc, rename to .zip, perform ops, rename back to xlsm

    Code:
    If IsZipFile(sZipFile) Then
        If Right$(sZipFile, 3) <> "zip" Then
            sHold = sZipFile
            sZipFile = RemoveExt(sZipFile) & ".zip"
            Name sHold As sZipFile
            bRename = True 'flag to rename later.
        End If
        SHCreateItemFromParsingName StrPtr(sZipFile), ByVal 0&, IID_IShellItem, psiRes
    The IsZipFile in this case then just has to be the more thorough version that examines the first few bytes for the zip file header;
    Code:
    Public Function IsZipFile(sFile As String) As Boolean
    On Error GoTo Done
    Dim aBytes() As Byte
    Dim pStrm As oleexp.IStream
    Dim hr As Long
    
    hr = SHCreateStreamOnFileEx(StrPtr(sFile), STGM_READ, FILE_ATTRIBUTE_NORMAL, 0&, 0&, pStrm)
    
    ReDim aBytes(3)
    pStrm.Read aBytes(0), 4
    If (aBytes(0) = &H50) And (aBytes(1) = &H4B) And (aBytes(2) = &H3) And (aBytes(3) = &H4) Then
        IsZipFile = True
    End If
    
    Done:
    Set pStrm = Nothing
    
    End Function
    Last edited by fafalone; Mar 6th, 2017 at 11:10 PM.

  7. #7
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: Read / write directly to zip

    If the application has been installed properly then UAC will probably end up creating a virtualized copy when you rename the file. Perhaps not serious but a little ugly.

    If these are user documents located in unprotected locations though that issue doesn't arise.

    Why not just store these as extended properties via the newer version of Dsofile.dll?

    The Dsofile.dll files lets you edit Office document properties when you do not have Office installed
    Last edited by dilettante; Mar 7th, 2017 at 04:58 AM.

  8. #8

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    Re: Read / write directly to zip

    dilettante

    this looks very interesting and can be exactly what I'm looking.

    Will take a deeper look into this. Thanks!!!
    My projects:
    Virtual Forms
    VBA Telemetry

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width