TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Pepe on November 25, 2009, 03:14:49 AM

Title: ZIP files ZIP folders & VLISP
Post by: Pepe on November 25, 2009, 03:14:49 AM
I suppose it's easy in other languages, but...

How to browse inside a 'zip' file using VLISP? and...

How to deal with it as a 'folder'?...How to create it?...How to decompress it?

Thanks!
Title: Re: ZIP files ZIP folders & VLISP
Post by: CAB on November 25, 2009, 09:40:15 AM
Have not seen one in vLisp.

Another direction
http://www.bzip.org/
http://www.faqs.org/docs/Linux-mini/Bzip2.html

Common lisp
http://www.cs.unm.edu/~madhu/clisp/binary-types/unzip.lisp
Title: Re: ZIP files ZIP folders & VLISP
Post by: alanjt on November 25, 2009, 11:18:08 AM
Didn't John (Se7en) post some code for compressing/decompressing files?
Title: Re: ZIP files ZIP folders & VLISP
Post by: CAB on November 25, 2009, 11:37:16 AM
Yes, good memory Alan.
http://www.theswamp.org/index.php?topic=14793.0
Title: Re: ZIP files ZIP folders & VLISP
Post by: alanjt on November 25, 2009, 11:40:16 AM
Yes, good memory Alan.
http://www.theswamp.org/index.php?topic=14793.0
:) You truly are the master of search.
Title: Re: ZIP files ZIP folders & VLISP
Post by: Pepe on November 25, 2009, 12:07:59 PM
Hi!

Thank you (very, very much) to you two for answering, Alan and CAB; useful links, but it wasn't exactly what I was looking for (I must be much more precise questioning. Language matters...I'm sorry... :oops:).

Windows XP has its own compressor and I suppose it belongs to that OS and, so, it should be easy to use it via 'VLAX-CREATE-OBJECT...etc...', but I can't find which OS (ActiveX...?) object, properties and methods are able for that (...maybe...(vlax-create-object "shell.application")...?).

Does anybody know anything about this?

Thank You again
Title: Re: ZIP files ZIP folders & VLISP
Post by: Keith™ on November 25, 2009, 03:03:39 PM
Here is my addition to the mix

Title: Re: ZIP files ZIP folders & VLISP
Post by: alanjt on November 25, 2009, 03:07:49 PM
Here is my addition to the mix


Daily, automatic zipped backups to my portable drive, here I come. Very nice Keith!
Title: Re: ZIP files ZIP folders & VLISP
Post by: Pepe on November 25, 2009, 03:39:12 PM
 :-o WOW!!!

That is exactly what I was looking for !

Thank you very much (a couple of virtual beers to your health), Keith!
Title: Re: ZIP files ZIP folders & VLISP
Post by: Lee Mac on November 25, 2009, 03:47:19 PM
Nice Keith  :wink:
Title: Re: ZIP files ZIP folders & VLISP
Post by: dgorsman on November 26, 2009, 02:17:25 PM
*yoink*   :mrgreen:

Much appreciated.  You might want to check on the leading comments: "Error hecking is limited..."   :lmao:
Title: Re: ZIP files ZIP folders & VLISP
Post by: Lee Mac on November 26, 2009, 02:28:27 PM
*yoink*   :mrgreen:

Much appreciated.  You might want to check on the leading comments: "Error hecking is limited..."   :lmao:

True... I read that as Error hacking...  but then who reads the headers these days...  :|
Title: Re: ZIP files ZIP folders & VLISP
Post by: Keith™ on November 26, 2009, 02:53:54 PM
Oh no, you can't be satisfied with the code, you gotta pick apart spelling errors in the header ... :P

well, if it is that bac, then don't use it ;)

I played with it a bit last night and realized that there is a problem with the utility to zip all files in a folder to a single file IF the zip file is located in the same folder in which all files are being compressed.

example:
You have a folder C:\Stuff\Pictures\ and you want to compress all of the files in that folder to a file called C:\Stuff\Pictures\Pictures.zip When using the built in Windows compression (or any other zip utility) you generally select the files prior to the compression taking place. In this utility, the files are compressed as they are selected, this means that C:\Stuff\Pictures\Pictures.zip will be selected to be compressed into itself, clearly this will not be possible.

A fix for that situation might be as follows:

Code: [Select]
(defun AddFolder2Zip (srcFolder destFile)
  (MakeEmptyZip destFile)
  (setq app (vlax-create-object "Shell.Application"))
  (setq folder (vlax-invoke app 'NameSpace srcFolder))
  (setq destZip (vlax-invoke app 'NameSpace destFile))
  (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))
    (if (/= (strcase (vlax-get-property file 'Name))(strcase(strcat (cadr (fnsplitl destFile))(caddr (fnsplitl destFile)))))
      (vlax-invoke destZip 'CopyHere file)
    ) 
    (setq ndx (1+ ndx))
  )
)

This will verify whether the zip file is actually being selected to be compressed and will skip it if needed.

I am adding additional error checking, and I have already determined that some systems handle the compression differently. For example, my home computer prompts you to overwrite the files in the zip file if they already exist, while my office computer will simply add the file again (thus there are 2 of the exact same files in the zip).

Determining if the file exists in the zip file should be as easy as:
Code: [Select]
(member (GetAllFilesInZip "MyFile.dwg"))

I am not sure how capitalization works in that scenario as I have not tested it.

I am glad everyone is finding it useful though.
Title: Re: ZIP files ZIP folders & VLISP
Post by: Pepe on November 27, 2009, 06:30:45 AM
Hi everybody!

I've been playing a while with all this stuff posted by Keith (nice work, indeed!).

A couple of questions:

First one: when zip file is just in "my documents" root path, it says "; error: <I can't transalte this part into english> : VLA-OBJECT nil"
Does anybody knows why?

Second one: zip files often contain folders and sub-folders.
Here is a little contribution to solve this. It's a simple browser of all the contents - including sub-folders - of the zip.

Code: [Select]
(defun moreinside (chkfile / cmt cnt fil its lst sbf)
  (if (= (vlax-get-property chkfile 'IsFolder) :vlax-true)
    (progn
      (setq sbf (vlax-get-property chkfile 'getfolder))
      (setq its (vlax-invoke-method sbf 'items))
      (setq cmt (vlax-get-property its 'count))
      (setq cnt 0)
      (repeat cmt
(setq fil (moreinside (vlax-invoke-method its 'item cnt)))
(setq lst (cons fil lst))
(setq cnt (1+ cnt))
)
      (vlax-release-object sbf)
      (vlax-release-object its)
      (gc)
      (cons (vla-get-name chkfile) (reverse lst))
      )
    (vla-get-name chkfile)
    )
  )
 
(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 (moreinside (vlax-invoke files 'Item ndx)))
    (setq filelist (append filelist (list file)))
    (setq ndx (1+ ndx))
    )
  (vlax-release-object folder)
  (vlax-release-object fso)
  (gc)
  filelist
  )

(defun showint (a b / str)
  (setq str "\n|-")
  (repeat b (setq str (strcat str "-")))
  (foreach n a
    (if (listp n)
      (progn
(princ (strcat str ">" (car n)))
(showint (cdr n) (1+ b))
)
      (princ (strcat str ">" n))
      )
    )
  )

(defun C:TESTZIPCONTENTS (/ cont filz)
  (setq filz (getfiled "Select a ZIP File" "" "zip" 8))
  (setq cont (GetAllFilesInZip filz))
  (textscr)
  (princ (strcat "\nContents of \"" (vl-filename-base filz) (vl-filename-extension filz) "\":\n|"))
  (showint cont 0)
  (princ)
  )

I've checked that "ExtractFileFromZip" function works properly for a file or folder if it is at the root of the contents of the zip file,
But, How to decompress a single file or sub-folder within a sub-folder? I've tried many ways to build the list of contents in order to do this, but without any success  :-(.
Title: Re: ZIP files ZIP folders & VLISP
Post by: Keith™ on November 27, 2009, 10:22:40 AM
I can successfully obtain a single folder and the files within that folder, but when there are nested folders, the method fails, even though the path is correct.

Example:
C:\MyZip.zip\SubFolder1 is returned and enumerated, however, C:\MyZip.zip\SubFolder1\Subfolder2 is not, in fact the 'NameSpace method fails here.

I'll have to investigate this more closely ...
Title: Re: ZIP files ZIP folders & VLISP
Post by: thanhduan2407 on December 21, 2017, 10:03:47 AM
Code: [Select]
(defun C:99 (/ DIRFILE1 FN1 NAMEFILE1 T1 TEN Z0 Z1)
  (setq Ten (getfiled "Select File" (getvar "dwgprefix") "*" 8))
  (setq fn1 (strcat (getvar "dwgprefix") Ten))
  (setq DirFile1 (vl-filename-directory fn1))
  (setq NameFile1 (vl-filename-base fn1))
  (setq t1 (strcat DirFile1 (chr 92) "doc.kml"))
  (vl-file-copy fn1 (strcat DirFile1 (chr 92) "doc.kml"))
  (setq Z0 (strcat DirFile1 (chr 92) "tempppppp111ggfssd.zip"))
  (MakeEmptyZip Z0)
  (if (/= (findfile t1) nil)
    (Add2Zip t1 Z0)
  )
  (wait 2.0)
  (setq Z1 (strcat DirFile1 (chr 92) NameFile1 "_.kmz"))
  (vl-file-copy
    Z0
    Z1
  )
  (if (/= (findfile fn1) nil)
    (vl-file-delete fn1)
  )
  (if (/= (findfile t1) nil)
    (vl-file-delete t1)
  )
  (if (/= (findfile Z0) nil)
    (vl-file-delete Z0)
  )
  (Princ)
)

(defun Add2Zip (srcFile destFile / app folder)
  (setq app (vlax-create-object "Shell.Application"))
  (setq folder (vlax-invoke app 'NameSpace destFile))
  (vlax-invoke folder 'CopyHere srcFile)
  (vlax-release-object folder)
  (vlax-release-object app)
)
(defun MakeEmptyZip (destFile / fso fo)
  (setq fso (vlax-create-object "Scripting.FileSystemObject"))
  (setq fo (vlax-invoke fso 'OpenTextFile destFile '2 'true))
  (vlax-invoke fo 'Write (strcat (chr 80) (chr 75) (chr 5) (chr 6)))
  (repeat 18 (vlax-invoke fo 'Write (chr 256)))
  (vlax-invoke fo 'Close)
  (vlax-release-object fo)
  (vlax-release-object fso)
)
;;(wait 0.01)
(defun wait (seconds / stop)
  (setq stop (+ (getvar "DATE") (/ seconds 86400.0)))
  (while (> stop (getvar "DATE")))
)


Title: Re: ZIP files ZIP folders & VLISP
Post by: Lee Mac on December 21, 2017, 01:38:43 PM
I believe Keith wrote those zip functions -
http://www.theswamp.org/index.php?topic=31097.msg366864#msg366864