home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.alaska-software.com
/
2014.06.ftp.alaska-software.com.tar
/
ftp.alaska-software.com
/
acsn
/
BDZIP.ZIP
/
BDZIP.PRG
< prev
next >
Wrap
Text File
|
2004-08-23
|
13KB
|
350 lines
///////////////////////////////////////////////////////////////////////////
// BDZip functions 2000 - Brent Dubs (bdubs@vanityshops.com)
// Xbase++ functions for using the addZIP compression library.
//
// If you do not have the XbToolsIII library, you need to replace
// the RAND function calls, they are only used to create an
// unique file name.
//
// To use these functions you will need the addZIP compression library
// available at http://www.littlebigware.com and you need the
// BAP library availible from the Alaska Software website in the
// Download/ASCN section.
//
// These functions will work with both the demo and registered
// versions of addZIP. You just need to uncomment the addZIP_Register
// calls and add your registration string you receive from addZIP.
//
// Version History:
// 08|23|2004 Minor corrections in calls to "addUNZIP_Test()",
// "addUNZIP_View()" (parameter was missing)
// 10|06|2004 Original Version by Brent Dubs
//
///////////////////////////////////////////////////////////////////////////
#include "common.ch"
#include "FileIO.ch"
#include "bap.ch"
#include "dll.ch"
#include "addzip.ch"
#pragma Library( "BAP.LIB" )
#define READFLATRECORD_BUFFERSIZE 1000
//-----------------------------------------------------------------------------
// The ZipFile function compresses a file or files into an archive (.ZIP) file.
//
// Parameters:
// xFile: File or files to compress. Can be a string (single file)
// or an array of strings (multiple files).
// cArcFile: Name of the archive file to create.
// nWindowHandle: Where to display the progress output (optional). You may
// pass either the window handle, or the object.
//
// Returns: True (.T.) if it successfully created the archive file.
// or False (.F.) if an error occured.
//
FUNCTION ZipFile(xFile,cArcFile,nWindowHandle)
LOCAL lValid := .F., nDllZip := 0, aFileList := {}, lFileMissing := .F.
LOCAL cFiles := ""
IF ValType(xFile) == "C"
aFileList := {xFile}
IF cArcFile == NIL
cArcFile := SUBSTR(xFile,1,AT(".",xFile)) + "ZIP"
ENDIF
ELSE
aFileList := xFile
ENDIF
AEVAL(aFileList,{|x|IF(!File(x),lFileMissing := .T.,NIL)})
IF ! lFileMissing
FOR I := 1 TO LEN(aFileList)
cFiles += ALLTRIM(aFileList[I]) + "|"
NEXT
nDllZip := DllLoad("azip32.dll")
IF nDllZip == 0
MSGBOX("Could not find the addZip azip32.dll file!","DLL missing")
ENDIF
DllCall(nDllZip,DLL_STDCALL,"addZIP_Initialise")
// Uncomment the next line and replace with your registration info. (If you want it registered)
// DllCall(nDllZip,DLL_STDCALL,"addZIP_Register","Your Name",0x????????)
IF nWindowHandle != NIL
IF VALTYPE(nWindowHandle) == "N"
DllCall(nDllZip,DLL_STDCALL,"addZIP_SetWindowHandle",nWindowHandle)
ELSE
DllCall(nDllZip,DLL_STDCALL,"addZIP_SetWindowHandle",nWindowHandle:getHwnd())
ENDIF
ENDIF
DllCall(nDllZip,DLL_STDCALL,"addZIP_Recurse",0)
DllCall(nDllZip,DLL_STDCALL,"addZIP_SaveStructure",0)
DllCall(nDllZip,DLL_STDCALL,"addZIP_ArchiveName",cArcFile)
DllCall(nDllZip,DLL_STDCALL,"addZIP_Include",cFiles)
DllCall(nDllZip,DLL_STDCALL,"addZIP")
DllUnload(nDllZip)
IF File(cArcFile)
lValid := .T.
ENDIF
ENDIF
RETURN lValid
//-----------------------------------------------------------------------------
// The UnZipFile function extracts files from an archive (.ZIP) file
//
// Parameters:
// cArcFile: Name of the archive file (.ZIP) to extract from.
// cPath: Directory where to extract the files to (optional).
// If not passed, then the files will be extracted into
// the current directory.
// lFullPath: Include the path in the returned results (optional)?
//
// Returns: An array of file names that were extracted from the archive
// file. If True (.T.) was passed to the lFullPath parameter,
// then the file names will include the path.
//
FUNCTION UnZipFile(cArcFile,cPath,lFullPath)
LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
LOCAL nHook := 0, cCall := "", nError := 0
DEFAULT lFullPath TO .F.
DEFAULT cPath TO ".\"
cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
UnZipCallBack(,,,cFileName)
nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
BaStruct(aOFN,nHook)
cOFN = BaAccess(aOFN)
nDllZip := DllLoad("aunzip32.dll")
IF nDllZip == 0
MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
ENDIF
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
// Uncomment the next line and replace with your registration info. (If you want it registered)
// DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
// This is where the callback is being used
cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
nError := DLLExecuteCall(cCall,nHook)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Include","*.*")
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_RestoreStructure",0)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ExtractTo",cPath)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
DllUnload(nDllZip)
UnZipCallBack(,,,,.T.)
nHandle := FOPEN(cFileName)
cRecord := ReadFlatRecord(nHandle)
DO WHILE !(cRecord == "")
IF lFullPath
AADD(aFileList,cRecord)
ELSE
AADD(aFileList,SUBSTR(cRecord,RAT("\",cRecord)+1))
ENDIF
cRecord := ReadFlatRecord(nHandle)
ENDDO
FCLOSE(nHandle)
FERASE(cFileName)
RETURN aFileList
//----------------------------------------------------------------------------
// The ViewZip function returns the name of the files contained in an
// archive file. It DOES NOT extract an archive file.
//
// Parameters:
// cArcFile: Name of the archive file (.ZIP) to view.
// lFullPath: Include the path in the returned results (optional)?
//
// Returns: An array of file names that are contained in the archive
// file. If True (.T.) was passed to the lFullPath parameter,
// then the file names will include the path.
//
FUNCTION ViewZip(cArcFile,lFullPath)
LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
LOCAL nHook := 0, cCall := "", nError := 0, lStatus := .T.
DEFAULT lFullPath TO .F.
cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
UnZipCallBack(,,,cFileName)
nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
BaStruct(aOFN,nHook)
cOFN = BaAccess(aOFN)
nDllZip := DllLoad("aunzip32.dll")
IF nDllZip == 0
MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
ENDIF
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
// Uncomment the next line and replace with your registration info. (If you want it registered)
// DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
// This is where the callback is being used
cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
nError := DLLExecuteCall(cCall,nHook)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_View", 1)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
DllUnload(nDllZip)
UnZipCallBack(,,,,.T.)
nHandle := FOPEN(cFileName)
cRecord := ReadFlatRecord(nHandle)
DO WHILE !(cRecord == "")
IF lFullPath
AADD(aFileList,SplitZipMessage(cRecord))
ELSE
AADD(aFileList,SUBSTR(cRecord,RAT("\",cRecord)+1))
ENDIF
cRecord := ReadFlatRecord(nHandle)
ENDDO
FCLOSE(nHandle)
FERASE(cFileName)
RETURN aFileList
//-------------------------------------------------------------------------
// The TestUnZip function determines if a file is a valid archive (.ZIP) file.
// It DOES NOT extract the archive file.
//
// Parameters:
// cArcFile: Name of the archive file (.ZIP) to test.
//
// Returns: True (.T.) if the file is a valid archive file or False (.F.)
// if the archive file is corrupted.
//
FUNCTION TestUnZip(cArcFile)
LOCAL nDllZip := 0, aOFN, cOFN, cRecord := "", aFileList := {}
LOCAL nHook := 0, cCall := "", nError := 0, lStatus := .T.
cFileName := "Z" + ALLTRIM(STR(INT(100000 * RAND(-1)) + 1)) + ".TMP"
UnZipCallBack(,,,cFileName)
nHook = BaCallBack("UnZipCallBack",BA_CB_ADDZIP)
aOFN = BaInit(1) // BAP.DLL from Gernot Trautmann
BaStruct(aOFN,nHook)
cOFN = BaAccess(aOFN)
nDllZip := DllLoad("aunzip32.dll")
IF nDllZip == 0
MSGBOX("Could not find the addZip aunzip32.dll file!","DLL missing")
ENDIF
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Initialise")
// Uncomment the next line and replace with your registration info. (If you want it registered)
// DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Register","Your Name",0x????????)
// This is where the callback is being used
cCall := DllPrepareCall(nDllZip,DLL_STDCALL,"addUNZIP_InstallCallback")
nError := DLLExecuteCall(cCall,nHook)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_ArchiveName",cArcFile)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP_Test", 1)
DllCall(nDllZip,DLL_STDCALL,"addUNZIP")
DllUnload(nDllZip)
UnZipCallBack(,,,,.T.)
nHandle := FOPEN(cFileName)
cRecord := ReadFlatRecord(nHandle)
DO WHILE !(cRecord == "")
AADD(aFileList,cRecord)
cRecord := ReadFlatRecord(nHandle)
ENDDO
FCLOSE(nHandle)
IF !EMPTY(aFileList)
lStatus := .F.
ENDIF
FERASE(cFileName)
RETURN lStatus
//-----------------------------------------------------------------------------
// The UnZipCallBack function is used internally by the UnZipFile, ViewZip
// and TestUnZip functions. You do not need to call it in your application.
//
FUNCTION UnZipCallBack(hwnd, nMsgNum, cMsg, cFileName, lClose)
STATIC nHandle := 0
DEFAULT lClose TO .F.
IF !(cFileName == NIL)
nHandle := FCREATE(cFileName)
ELSE
IF lClose
FCLOSE(nHandle)
ELSE
DO CASE
CASE nMsgNum == ADDZIP_ERROR
FWRITE(nHandle,cMsg+CHR(13)+CHR(10))
CASE nMsgNum == ADDZIP_VIEW
FWRITE(nHandle,cMsg+CHR(13)+CHR(10))
CASE nMsgNum == ADDZIP_UNZIPPED
FWRITE(nHandle,SplitZipMessage(cMsg)[2]+CHR(13)+CHR(10))
ENDCASE
ENDIF
ENDIF
RETURN NIL
//----------------------------------------------------------
// The SplitZipMessage function is used internally by the ViewZip and
// UnZipCallBack functions. You do not need to call it in your application.
//
FUNCTION SplitZipMessage(cMsg)
LOCAL aZipInfo := {}, nPos := AT("|",cMsg)
WHILE !(nPos == 0)
AADD(aZipInfo,SUBSTR(cMsg,1,nPos-1))
cMsg := SUBSTR(cMsg,nPos+1)
nPos := AT("|",cMsg)
ENDDO
RETURN aZipInfo
//-----------------------------------------------------------------------------
// The ReadFlatRecord function is used internally by the UnZipFile, ViewZip,
// and TestUnZip functions. You do not need to call it in your application.
// It does not use any addZIP functions, it is just used to read a record
// from a text file.
//
FUNCTION ReadFlatRecord(nFileHandle,nRecNum,cErrorRecord)
LOCAL nPos := 0, cRecord := "", I := 0, lError := .F., nRecLen := 0
IF cErrorRecord == NIL
cRecord := ""
ENDIF
IF nRecNum != NIL
IF nRecNum > 1
I := 1
FSEEK(nFileHandle,0,FS_SET)
WHILE !lError .AND. (I < nRecNum)
cRecord := FREADSTR(nFileHandle,READFLATRECORD_BUFFERSIZE)
nRecLen := LEN(cRecord)
nPos := AT(CHR(13),cRecord)
IF (LEN(cRecord) == READFLATRECORD_BUFFERSIZE) .OR. (nPos != 0)
FSEEK(nFileHandle,((nPos +1)-nRecLen),FS_RELATIVE)
++I
ELSE
lError := .T.
cRecord := ""
ENDIF
ENDDO
ELSE
FSEEK(nFileHandle,0,FS_SET)
ENDIF
ENDIF
cRecord := FREADSTR(nFileHandle,READFLATRECORD_BUFFERSIZE)
nRecLen := LEN(cRecord)
nPos := AT(CHR(13),cRecord)
IF (LEN(cRecord) == READFLATRECORD_BUFFERSIZE) .OR. (nPos != 0)
cRecord := SUBSTR(cRecord,1,nPos-1)
FSEEK(nFileHandle,((nPos +1)-nRecLen),FS_RELATIVE)
ELSE
cErrorRecord := cRecord
cRecord := ""
ENDIF
RETURN cRecord