Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need to unzip gz files
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Contrôles ActiveX en VFP
Divers
Thread ID:
01448567
Message ID:
01448681
Vues:
55
>Hello All,
>
>Does anyone know any good (inexpensive?) controls for unzipping gz files?
>
>Thank You Very Much!
Here's what I use in Web Connection and Client Tools. It was based on some code that James Blackburn posted some time ago on his blog (I can't find the original entry).

It uses the free zlib.dll which you can find here:
http://www.zlib.net/
************************************************************************
* GZipCompressString
****************************************
***  Function: Compresses a string using GZip
***    Assume: Requires ZLIB1.DLL 
***      Pass:
***    Return:
************************************************************************
FUNCTION GZipCompressString(lcString,lnCompressionLevel)
LOCAL lcOutput, lcOutFile,lcInFile, lnHandle

*** 1 - 9
IF EMPTY(lnCompressionLevel)
   lnCompressionLevel = -1  && Default
ENDIF

*** Must write to files
lcOutFile = SYS(2023) + SYS(2015) + TRANS(Application.ProcessId) + ".gz"
lcInFile = lcOutFile + ".in"

*** Failure to write the file
IF !FILE2VAR(lcInFile,lcString)
   RETURN ""
ENDIF

IF !VARTYPE(_GZipLoaded) = "L"
	GzipLibraries()
ENDIF

TRY
   lnHandle = gzopen(lcOutFile,"wb")
   IF (lnHandle < 0)
      RETURN ""
   ENDIF

   *** Set the compression level
   gzsetparams(lnHandle,lnCompressionLevel,0)

   gzwrite(lnHandle,lcString,LEN(lcString))
   gzclose(lnHandle)
CATCH
   IF lnHandle > -1
      gzclose(lnHandle)
   ENDIF
ENDTRY

lcOutput = FILETOSTR(lcOutFile)

ERASE (lcOutFile)
ERASE (lcInFile)

RETURN lcOutput


************************************************************************
* GZipUncompressString
****************************************
***  Function: Uncompresses a GZip string
***    Assume: 
***      Pass: lcCompressed   -  Compressed String
***            llIsFile       -  if .T. lcCompressed is a file
***    Return: decompressed string
************************************************************************
FUNCTION GZipUncompressString(lcCompressed,llIsFile)
LOCAL lcInFile, lcOutput, lnHandle

IF llIsFile
   *** Use parameter file name as input
   lcInFile = lcCompressed
ELSE
   lcInFile = ADDBS(SYS(2023)) + SYS(2015) + TRANSFORM(Application.ProcessId) + ".gz"
   *** Copy file to disk and use as input
   FILE2VAR(lcInFile,lcCompressed)
ENDIF

IF !VARTYPE(_GZipLoaded) = "L"
	GzipLibraries()
ENDIF

lcOutput = ""
TRY
   lnHandle = gzopen(lcInFile,"rb")
   IF (lnHandle < 1)
      RETURN ""
   ENDIF

   lcOutput = ""
   DO WHILE .T.
      lcBuffer = SPACE(65535)
      lnResult = gzread(lnHandle,@lcBuffer,LEN(lcBuffer))
      IF lnResult < 1
         EXIT
      ENDIF
      lcOutput = lcOutput + LEFT(lcBuffer,lnResult)
   ENDDO
CATCH
   * Nothing
FINALLY
   IF lnHandle > 0	
	   gzclose(lnHandle)
   ENDIF
   ERASE(lcInFile)
ENDTRY

RETURN lcOutput
* eof GZipUncompressString

************************************************************************
* GzipLibraries
****************************************
***  Function:
***    Assume:
***      Pass:
***    Return:
************************************************************************
FUNCTION GzipLibraries()

PUBLIC _GZipLoaded
_GZipLoaded=.T.

* Opens file for writing
DECLARE LONG gzopen IN zlib1.dll ;
   STRING @ zFile ,;
   STRING @ zMode

* Writes data from a compressed file - gzip
DECLARE LONG gzwrite IN zlib1.dll ;
   LONG FILE ,;
   STRING @ uncompr,;
   LONG uncomprLen

*** Set options on the compression
DECLARE LONG gzsetparams IN zlib1.DLL ;
   LONG  gzFile,;
   INTEGER LEVEL,;
   INTEGER strategy

DECLARE LONG gzread IN zlib1.dll  ;
   LONG gzFile,;
   STRING @ buf,;
   LONG LEN

* Closes the file
DECLARE LONG gzclose IN zlib1.dll ;
   LONG FILE

RETURN
If you use Web Connection, IPStuff or Client Tools wwAPI.prg contains these standalone functions.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform