#command COMPRESS [ FILES ] <afiles> ;
TO <zipfile> ;
BLOCK <block> ;
[ <ovr: OVERWRITE> ] ;
[ <srp: STOREPATH> ] ;
[ PASSWORD <password> ] ;
=> ;
COMPRESSFILES ( <zipfile> , <afiles>, <block> , <.ovr.> , <.srp.> , <password> )
Function Main
// chama as funções para zipar e unzipar
Return Nil
#command UNCOMPRESS [ FILE ] <zipfile> ;
[ BLOCK <block> ] ;
[ PASSWORD <password> ] ;
=> ;
UNCOMPRESSFILES ( <zipfile> , <block> , <password> )
*------------------------------------------------------------------------------*
Function CreateZip()
*------------------------------------------------------------------------------*
Local aDir := Directory( "f*.txt", "D" ), aFiles:= {}, nLen
Local cPath := CurDrive()+":\"+CurDir()+"\"
FillFiles( aFiles, aDir, cPath )
if ( nLen := Len(aFiles) ) > 0
Form_1.ProgressBar_1.RangeMin := 1
Form_1.ProgressBar_1.RangeMax := nLen
MODIFY CONTROL Label_1 OF Form_1 FONTCOLOR {0,0,0}
COMPRESS aFiles ;
TO 'ZipTest.Zip' ;
BLOCK {|cFile, nPos| ProgressUpdate( nPos, cFile, .T. ) } ;
PASSWORD "mypass" ;
OVERWRITE
MODIFY CONTROL Label_1 OF Form_1 FONTCOLOR {0,0,255}
Form_1.Label_1.Value := 'Backup is finished'
endif
Return Nil
*------------------------------------------------------------------------------*
Function ProgressUpdate( nPos , cFile , lShowFileName )
*------------------------------------------------------------------------------*
Default lShowFileName := .F.
Form_1.ProgressBar_1.Value := nPos
Form_1.Label_1.Value := cFileNoPath( cFile )
if lShowFileName
INKEY(.2)
endif
Return Nil
*------------------------------------------------------------------------------*
Function UnZip()
*------------------------------------------------------------------------------*
Local cCurDir := GetCurrentFolder(), cArchive
cArchive := Getfile ( { {'Zip Files','*.ZIP'} } , 'Open File' , cCurDir , .f. , .t. )
if !Empty(cArchive)
Form_1.ProgressBar_1.RangeMin := 0
Form_1.ProgressBar_1.RangeMax := GetFilesCountInZip( cArchive )
MODIFY CONTROL Label_1 OF Form_1 FONTCOLOR {0,0,0}
UNCOMPRESS cArchive ;
BLOCK {|cFile, nPos| ProgressUpdate( nPos, cFile, .T. ) } ;
PASSWORD "mypass"
MODIFY CONTROL Label_1 OF Form_1 FONTCOLOR {0,0,255}
Form_1.Label_1.Value := 'Restoration of Backup is finished'
endif
Return Nil
*------------------------------------------------------------------------------*
Function FillFiles( aFiles, cDir, cPath )
*------------------------------------------------------------------------------*
Local aSubDir, cItem
FOR cItem :=1 TO LEN(cDir)
IF cDir[cItem][5] <> "D"
AADD( aFiles, cPath+cDir[cItem][1] )
ELSEIF cDir[cItem][1] <> "." .AND. cDir[cItem][1] <> ".."
aSubDir := DIRECTORY( cPath+cDir[cItem][1]+"\*.*", "D" )
aFiles:=FillFiles( aFiles, aSubdir, cPath+cDir[cItem][1]+"\" )
ENDIF
NEXT
Return aFiles
*------------------------------------------------------------------------------*
Function GETFILESCOUNTINZIP ( cFileName )
*------------------------------------------------------------------------------*
LOCAL i := 0 , hUnzip , nErr
hUnzip := HB_UNZIPOPEN( cFileName )
nErr := HB_UNZIPFILEFIRST( hUnzip )
DO WHILE nErr == 0
i++
nErr := HB_UNZIPFILENEXT( hUnzip )
ENDDO
HB_UNZIPCLOSE( hUnzip )
Return i
*------------------------------------------------------------------------------*
PROCEDURE COMPRESSFILES ( cFileName , aDir , bBlock , lOvr , lStorePath , cPassword )
*------------------------------------------------------------------------------*
LOCAL hZip , cZipFile , i
if valtype (lOvr) == 'L'
if lOvr == .t.
if file (cFileName)
delete file (cFileName)
endif
endif
endif
hZip := HB_ZIPOPEN( cFileName )
IF ! EMPTY( hZip )
FOR i := 1 To Len (aDir)
if valtype (bBlock) == 'B'
Eval ( bBlock , aDir [i] , i )
endif
cZipFile := if( lStorePath, aDir [i], cFileNoPath( aDir [i] ) )
HB_ZipStoreFile( hZip, aDir [i], cZipFile, cPassword )
NEXT
ENDIF
HB_ZIPCLOSE( hZip )
RETURN
*------------------------------------------------------------------------------*
PROCEDURE UNCOMPRESSFILES ( cFileName , bBlock , cPassword )
*------------------------------------------------------------------------------*
LOCAL i := 0 , hUnzip , nErr, cFile, dDate, cTime, nSize, nCompSize, lCrypted, cComment
hUnzip := HB_UNZIPOPEN( cFileName )
nErr := HB_UNZIPFILEFIRST( hUnzip )
DO WHILE nErr == 0
HB_UnzipFileInfo( hUnzip, @cFile, @dDate, @cTime,,,, @nSize, @nCompSize, @lCrypted, @cComment )
if valtype (bBlock) == 'B'
Eval ( bBlock , cFile , ++i )
endif
HB_UnzipExtractCurrentFile( hUnzip, NIL, cPassword )
nErr := HB_UNZIPFILENEXT( hUnzip )
ENDDO
HB_UNZIPCLOSE( hUnzip )
RETURN