Wiki Home

XPZip Support


Namespace: People
Trying to use XP's Compressed Folder (zip file, not to be confused with NTFS compression.)

The good news is I can zip up a bunch of files.
The bad news is I can't figure out how to specify the path to store in the zip; like what you get with pkzip -rp.

Here is what I have so far:

* zippy.prg
objShell = CreateObject("Shell.Application")

lcCurDir = FullPath("")

* normal dir:
objFolder = objShell.NameSpace( lcCurDir )
objFolder.NewFolder("TestFolder")

*-- Create the zip file
lcZipFileName = FullPath( "test.zip" )
Erase (lcZipFileName)
lcZipFile = Chr(80) + Chr(75) + Chr(5) + Chr(6) + Replicate(Chr(0), 18)
Strtofile( lcZipFile, lcZipFileName )

* Create a dummy file to put in the zip
* RmDir "test"
* MkDir "test"
lcDummyFileName = FullPath("test\test.txt")
Strtofile( "zipme", lcDummyFileName )
lcDummyFileName2 = FullPath("test2.txt")
Strtofile( "zipme", lcDummyFileName2 )

* Zip Dir
oZip = objShell.NameSpace( lcZipFileName )

* Put the dummyfile in the zip
oZip.CopyHere( lcDummyFileName )

* So far so good.  Now for the part that doent work:

* Attempt to create a subdir (only way I can see to specify the path the file should unzip to)

oZip.NewFolder("TestFolder")  && Error# 1440, "OLE exception error: Exception code c0000005. OLE object may be corrupt"

oZip.MoveHere( FullPath("test\") )
* if test\ is empty:
&& Compressed (zipped) Folders Error
&& The specified directory D:\dev\util\pjx\trakproj\test is empty,
&& so Compressed (zipped) Folders cannot add it to the archive.
* putting test.txt into test\ allows it to work.

oTest = objShell.NameSpace(FullPath( "test.zip\test"))
oTest.MoveHere( lcDummyFileName2 ) && Error# 1924, "OTEST is not an object."



Any ideas how to overcome the problem with empty folders? And how zip just particular file and not the folder's content?

I have a really nice class that abstracts everthing so that you just call .AddToList( filename ) and .ZipList(), but not being able to specify a destination path is really a drag.

One way to make it work: copy all the files to \temp\tobezipped, then move that dir into the zip. But I am hoping there is a better way.
Carl Karsten


The problem is in the reference. By changing the reference to
the folder object I can call the NewFolder Method. Next, I grab
a reference to the new folder and then unzip all items
to my new destination.


LOCAL loFolder, loDestination

* we create a reference to the folder object (current directory)
loFolder = objShell.NameSpace( ADDBS(lcCurDir) )
loFolder.NewFolder("TestFolder")

lcNewFolder = lcCurDir+"TestFolder\" && path to new folder

* we can now reference our new folder
loDestination = objShell.NameSpace( lcNewFolder )

* Copy the files into the newly created folder
loDestination.CopyHere(  oZip.items )



Carla Fair Wright



Good idea. I created base class this

DEFINE CLASS ZIP as Custom
	HIDDEN nomezip,nomediretorio
	nomezip=.null.
	nomediretorio=.null.
	erro=""
	FUNCTION init as VOID
		IF TYPE("_zipshell")#"U"
			RETURN
		endif
		PUBLIC _zipshell as Object
		_zipshell=createobject("Shell.Application")
		RETURN
	ENDFUNC
	FUNCTION arquivozip (m.lcArquivo as String) as Boolean
		IF !FILE(m.lcArquivo)
			STRTOFILE(Chr(80)+Chr(75)+Chr(5)+Chr(6)+Replicate(Chr(0),18),m.lcArquivo)
		endif
		this.nomezip=_zipshell.namespace(m.lcArquivo)
		RETURN !ISNULL(this.nomezip)
	ENDFUNC
	FUNCTION diretorio (m.lcDiretorio as String) as Boolean
		this.nomediretorio=_zipshell.namespace(m.lcDiretorio)
		RETURN !ISNULL(this.nomediretorio)
	ENDFUNC
	FUNCTION zip as Boolean
		IF !this.verifica()
			RETURN .f.
		ENDIF
		this.nomezip.copyhere(this.nomediretorio)
		RETURN .t.
	ENDFUNC
	FUNCTION unzip as Boolean
		IF !this.verifica()
			RETURN .f.
		ENDIF
		this.nomediretorio.copyhere(this.nomezip.items)
	endfunc
	PROTECTED FUNCTION verifica as Boolean
		IF ISNULL(this.nomezip)
			this.erro="Arquivo ZIP não existe"
			RETURN .f.
		ENDIF
		IF ISNULL(this.nomediretorio)
			this.erro="Diretorio nao existe"
			RETURN .f.
		ENDIF
	ENDFUNC
ENDDEFINE



Example use:
objZip=CREATEOBJECT("zip")
&& create ZIP
objzip.arquivozip("d:\work\meuzip.zip")
objzip.diretorio("d:\teste")
IF !objzip.zip()
	MESSAGEBOX(objzip.erro)
endif

&& UNZIP
objzip.arquivozip("d:\work\meuzip.zip")
objzip.diretorio("d:\")
IF !objzip.zip()
	MESSAGEBOX(objzip.erro)
endif

Fabiano Costa
( Topic last updated: 2006.09.06 12:18:12 AM )