Author Topic: Extract and read files from ZIP  (Read 1582 times)

0 Members and 1 Guest are viewing this topic.

Lastknownuser

  • Newt
  • Posts: 29
Extract and read files from ZIP
« on: September 29, 2021, 09:08:47 AM »
Hi, I'm looking for help with accessing zipped files. I have a zip file that contains few more folders with txt files in them, and I'd need to extract some data from them.
So far I've found out that I can access zipped files by extracting them first, but really have no idea haw to do that. So any help and tips would be welcome. Preferably if it is possible to a temp folder.

I have read this topic: http://www.theswamp.org/index.php?topic=31097.msg366864#msg366864
I tried lisp written by user Keith, ExtractAllFilesFromZip, but its giving me "bad argument type: VLA-OBJECT nil" error.

JohnK

  • Administrator
  • Seagull
  • Posts: 10656
Re: Extract and read files from ZIP
« Reply #1 on: September 29, 2021, 09:35:46 AM »
I believe Keith's utilities used the windows methods (built in) for reading/writing zip files so something may have changed on the Windows side causing that error. Or you just used his utility wrong. ...You would have to tell us how you are using his routines.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

BIGAL

  • Swamp Rat
  • Posts: 1429
  • 40 + years of using Autocad
Re: Extract and read files from ZIP
« Reply #2 on: September 29, 2021, 08:36:52 PM »
You can unzip files as a windows command trying to find it something like unzip filename destination. I was looking at using it to provide end users a zip file and have a lisp that would unzip it to a directory that I had decided to use.

(startapp "c:\\program files\\7-zip\\7z x c:\\temp\\project2.zip -o c:\\temp\\ziptest")
A man who never made a mistake never made anything

Lastknownuser

  • Newt
  • Posts: 29
Re: Extract and read files from ZIP
« Reply #3 on: September 30, 2021, 01:55:07 AM »
I believe Keith's utilities used the windows methods (built in) for reading/writing zip files so something may have changed on the Windows side causing that error. Or you just used his utility wrong. ...You would have to tell us how you are using his routines.

I tried using it like this, maybe I'm doing something wrong

Code: [Select]
(defun c:test ()

(setq zipfile (getfiled "Select ZIP" "" "zip" 8))
(ExtractAllFilesFromZip zipfile "D:\test") 

  )
;;;   (ExtractAllFilesFromZip "source file.zip with path" "destination path")
;;;   (GetAllFilesInZip "source file.zip with path") returns a list of all files in the zip file

(defun ExtractAllFilesFromZip (zipFile strDest / folder fso)
  (setq filelist (GetAllFilesInZip zipfile))
  (setq fso (vlax-create-object "Shell.Application"))
  (setq folder (vlax-invoke fso 'NameSpace strDest))
  (setq ndx 0)
  (repeat (length filelist)
    (vlax-invoke folder 'CopyHere (strcat zipFile "\\" (nth ndx filelist)))
    (setq ndx (1+ ndx))
  )
  (vlax-release-object folder)
  (vlax-release-object fso)
)
(defun GetAllFilesInZip (zipFile / count file filelist files folder fso ndx path)
  (setq path (car (fnsplitl zipFile)))
  (setq fso (vlax-create-object "Shell.Application"))
  (setq folder (vlax-invoke fso 'NameSpace zipFile))
  (setq files (vlax-invoke folder 'Items))
  (setq count (vlax-get-property files 'Count))
  (setq ndx 0)
  (repeat count
    (setq file (vlax-invoke files 'Item ndx))
    (setq filelist (append filelist (list (vlax-get-property file 'Name ))))
    (setq ndx (1+ ndx))
  )
  filelist
)

Lastknownuser

  • Newt
  • Posts: 29
Re: Extract and read files from ZIP
« Reply #4 on: September 30, 2021, 05:48:22 AM »
I actually figure out it works, sometimes. I noticed that it extracts only files that do not have extension, for example .txt files are not extracted for some reason, and I just get number 0 written on command line. When there is a folder inside zip file I get this error on command line: bad argument type: stringp ("Folder_name"). Does it have anything to do with getfiled flag?

JohnK

  • Administrator
  • Seagull
  • Posts: 10656
Re: Extract and read files from ZIP
« Reply #5 on: September 30, 2021, 10:30:49 AM »
I'll try to get a moment to look at your code in a bit. That's weird that you get odd results.

In the meantime you can try an example I setup. It's not fancy but it was meant to offer a baseline to use an external app (like ZIP in this case). This may be easier for you to use.

https://www.theswamp.org/index.php?topic=14793.0
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lastknownuser

  • Newt
  • Posts: 29
Re: Extract and read files from ZIP
« Reply #6 on: October 01, 2021, 02:27:39 AM »
I got it to work using someone else's code I found here:
Code: [Select]
(defun ExtractFilesFromZip
       (zipFileName Destination / File Filelist Folder Fso Index Items Path)
  ;;CodeHimBelonga kdub@theSwamp
  ;;
  (setq filelist '()
        Path     (car (fnsplitl zipFileName))
        Fso      (vlax-create-object "Shell.Application")
        Folder   (vlax-invoke fso 'NameSpace zipFileName)
        Items    (vlax-invoke Folder 'Items)
        Index    0
  )
  (repeat (vlax-get-property Items 'Count)
    (setq File     (vlax-invoke Items 'Item Index)
          Filelist (append FileList (list (vlax-get-property File 'Name)))
          Index    (1+ Index)
    )
  )
  (setq Folder (vlax-invoke Fso 'NameSpace Destination)
        Index  0
  )
  (repeat (length FileList)
    (vlax-invoke Folder
                 'CopyHere
                 (strcat zipFileName "\\" (nth Index FileList))
    )
    (setq Index (1+ Index))
  )
  (vlax-release-object Folder)
  (vlax-release-object Items)
  (vlax-release-object Fso)
  Index
)
(princ)

Now, is there a way to extract files (txt) to temp folder? Basically I want to read from txt files from zip, by just selecting zip file. So I had in mind something like that, extract them to temp folder, or is it possible to extrct them, read from txt and then delete files, all that by lisp code?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Extract and read files from ZIP
« Reply #7 on: October 01, 2021, 02:43:53 AM »
I tried using it like this, maybe I'm doing something wrong
(ExtractAllFilesFromZip zipfile "D:\test")
Should be:
(ExtractAllFilesFromZip zipfile "D:\\test")