Solved

Using LZRead API Call....

Posted on 1998-04-22
4
843 Views
Last Modified: 2012-08-13
Hi there,

I'm interested in using the LZRead API call to load a compressed file into memory; i've checked various locations and have managed to find C++ source code to achieve this.  Seeing as my C++ skills are severely limited the sourcecode is little or no use to me.

Anyone here know how to use the LZRead function to load the data into memory - the API guide i've got says that it can be done by using LZRead and LZSeek.

Thanks in advance

Pete
0
Comment
Question by:peterwest
  • 2
4 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 1427027
If possible, could you paste the C code to a comment and display it - I know C and VB.
0
 
LVL 2

Author Comment

by:peterwest
ID: 1427028
Hi again,

Here's the main C++ code along with the contents of the global.h file.  If you need any of the other files then give me a shout.

Thanks

Pete

GLOBAL.H
---------
//*************************************************************
//  File name: global.h
//
//  Description:
//      Global include file for #defines and prototypes
//
//  History:    Date       Author     Comment
//               2/16/92   EMB        Created
//                
//  Copyright (c) 1992 Microsoft Corporation. All rights reserved.
//*************************************************************

#include <windows.h>
#include <lzexpand.h>
#include <commdlg.h>
#include <memory.h>

//*** Constants
#define FILENAMELEN 68
        // 64 (max MS DOS path+filename length) + 3 (drive) + 1 (NULL terminator)
#define LONGCHARLEN 12
        // number of chars needed for "longest" long (-2147483648) + NULL terminator
#define MAXREAD 4000
        // maximum bytes to read from file
   
//*** Menu Defines
    #define IDM_ABOUT 200
    #define IDM_OPEN 201
    #define IDM_COPY 202
    #define IDM_MULTCOPY 203


//*** Function Prototypes
//*** Main.c
    int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
    long FAR PASCAL MainWndProc(HWND, unsigned, WORD, LONG);
    BOOL FAR PASCAL About(HWND, unsigned, WORD, LONG);
    HANDLE FAR GetOpenFileHandle(HWND,LPSTR,LPSTR);
    HANDLE FAR GetSaveFileHandle(HWND,LPSTR);

//*** Init.c
    BOOL InitApplication(HANDLE);
    BOOL InitInstance(HANDLE, int);

//*** EXTERNS for Global Variables
    extern HANDLE       ghInst;
    extern HWND         ghWndMain;
    extern HWND ghWndEdit;

    extern char         szMainMenu[];
    extern char         szMainClass[];

//*** EOF: global.h

Main.c
------
//*************************************************************
//  File name: main.c
//
//  This sample demonstrates the use of the functions in LZEXPAND.DLL.
//  LZEXPAND.DLL contains functions to decompress data which was
//  previously compressed by COMPRESS.EXE, the DOS program based on the
//  Lempel-Ziv compression algorithm.
//  
//  The sample has three menu options for accessing files - Open, Copy 1,
//  and Copy Multiple.  The Open and Save As common dialogs are used get
//  the file names.  If the user selects to open a file, it is opened
//  with LZOpenFile, and read with LZRead.  If the file is compressed,
//  LZRead will decompress it.  The uncompressed file is displayed in a
//  read only edit control which fills the client area. The file is
//  closed with LZClose.
//  
//  If the user selects to copy 1 file, the source and destination are
//  opened with LZOpenFile, the source is copied (and decompressed if
//  necessary) with LZCopy, and both files are closed with LZClose.
//  
//  If the user selects to copy multiple files, the sample uses LZStart
//  to allocate memory for the multiple file copies, LZOpenFile to open
//  the source and destination files, CopyLZFile to copy, and LZClose to
//  close the files.  LZDone frees the memory allocated by LZStart.
//  
//
//  Functions:
//     WinMain - entry point
//     MainWndProc - main window procedure
//     About - About dialog box procedure
//     GetOpenFileHandle - puts up an Open common dialog, and opens
//                         the selected file
//     GetSaveFileHandle - puts up a Save As common dialog, and opens
//                         specified file.
//
//
//  History:    Date       Author         Comment
//               2/13/92   Emily Brooks   Created
//
// Written by Microsoft Product Support Services, Windows Developer Support
// Copyright (c) 1992 Microsoft Corporation. All rights reserved.
//*************************************************************

#include "global.h"

HANDLE ghInst = NULL;   // instance
HWND ghWndMain = NULL;  // handle of main window
HWND ghWndEdit = NULL;  // handle of edit control filling main window's client area

char szMainMenu[] = "MainMenu";
char szMainClass[] = "MainClass";

//*************************************************************
//
//  WinMain()
//
//  Purpose:
//            Entry point for all windows apps
//
//
//  Parameters:
//      HANDLE hInstance
//      HANDLE hPrevInstance
//      LPSTR lpCmdLine
//      int nCmdShow
//      
//
//  Return: (int PASCAL)
//
//
//  Comments:
//
//
//  History:    Date       Author     Comment
//               2/16/92   EMB        Created
//
//*************************************************************

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   MSG msg;

   if (!hPrevInstance && !InitApplication(hInstance))
      return (FALSE);

   if (!InitInstance(hInstance, nCmdShow))
      return (FALSE);

   while (GetMessage(&msg, NULL, NULL, NULL))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (msg.wParam);
}   //*** WinMain()

//*************************************************************
//
//  MainWndProc()
//
//  Purpose:
//            Main Window procedure
//
//
//  Parameters:
//      HWND hWnd
//      unsigned msg
//      WORD wParam
//      LONG lParam
//      
//
//  Return: (long FAR PASCAL)
//
//
//  Comments:
//
//
//  History:    Date       Author     Comment
//               2/16/92   EMB        Created
//
//*************************************************************

long FAR PASCAL MainWndProc (HWND hWnd, unsigned msg, WORD wParam, LONG lParam)
{
   FARPROC lpProc;
   RECT rect;

   switch (msg)
   {
      case WM_CREATE:
      {
         // create read only edit control to display any file user opens.
         // make it fill the client area
         GetClientRect(hWnd, &rect);

         ghWndEdit = CreateWindow("edit",
                                  "",
                                  WS_CHILD | ES_MULTILINE | WS_BORDER | WS_VSCROLL | WS_VISIBLE | ES_READONLY,
                                  rect.left, rect.top, rect.right, rect.bottom,
                                  hWnd, NULL, ghInst, NULL);
      }
         break;

      case WM_SIZE:  // resize edit control to fill the resized client area
      {
         GetClientRect(hWnd, &rect);
         MoveWindow(ghWndEdit, 0, 0, rect.right, rect.bottom, TRUE);
      }
         break;

      case WM_COMMAND:
         switch (wParam)
         {
            case IDM_OPEN:  // read a possibly compressed file into the edit control
            {

               LONG lfilesize;
               char szOrigName[FILENAMELEN];
               HANDLE hFile;
               HANDLE hBuf;
               LPSTR lpBuf;
               LONG ltoread, lnumread;
               char szFileSize[LONGCHARLEN+9];


               // find out which file and open it
               hFile = GetOpenFileHandle(hWnd, (LPSTR)szOrigName, (LPSTR)"Open");
               if (hFile != -1)
               {
                  // what's the file size
                  lfilesize = LZSeek(hFile, 0L, 2);

                  // display the file name and size in the main window's caption
                  wsprintf(szFileSize, " - %ld bytes", lfilesize);
                  lstrcat(szOrigName, szFileSize);
                  SetWindowText(hWnd, szOrigName);

                  // read at most MAXREAD bytes of the file
                  ltoread = min(lfilesize,MAXREAD);
                  if (hBuf = GlobalAlloc(GMEM_MOVEABLE, ltoread + 1))
                  {
                     if (lpBuf = GlobalLock(hBuf))
                     {
                        LZSeek(hFile, 0L, 0);  // seek to beginning of file
                        lnumread = LZRead(hFile, lpBuf, (int)ltoread);  // read
                        lpBuf[lnumread] = '\0';

                        // display the file contents in the edit control
                        SetWindowText(ghWndEdit, lpBuf);
                        GlobalUnlock(hBuf);
                     }
                     GlobalFree(hBuf);
                  }
                  LZClose(hFile); // close the file
               }
            }
               break;

            case IDM_COPY:  // copy 1 file
            {
               char szOrigName[FILENAMELEN];
               HANDLE hSource, hDest;
               char buf[FILENAMELEN+8];


               // find out which file is to copied and open it
               if ((hSource = GetOpenFileHandle(hWnd, (LPSTR)szOrigName, (LPSTR)"Copy from")) == -1)
                  break;

               // find out destination file name, and open the file
               wsprintf(buf, "Copy %s to", (LPSTR)szOrigName);
               if ((hDest = GetSaveFileHandle(hWnd, buf)) == -1)  // abort
               {
                  LZClose(hSource);
                  break;
               }

               // copy source to dest, decompressing if necessary
               if (hSource != -1 && hDest != -1)
               {
                  LZCopy(hSource, hDest);  // copy

                  // close both source and dest
                  LZClose(hSource);
                  LZClose(hDest);
               }
            }


               break;

            case IDM_MULTCOPY:  // copy multiple files
            {
               char szOrigName[FILENAMELEN];
               HANDLE hSource, hDest;
               char buf[FILENAMELEN+8];


               // allocate memory for multiple file copy
               LZStart();

               // find out which file is to copied and open it.
               // Do this till user hits cancel from either the Open or Save As common dialog
               while ((hSource = GetOpenFileHandle(hWnd, (LPSTR)szOrigName, (LPSTR)"Copy from")) != -1)
               {
                  // find out destination file name, and open the file
                  wsprintf(buf, "Copy %s to", (LPSTR)szOrigName);
                  if ((hDest = GetSaveFileHandle(hWnd, buf)) == -1) // abort
                  {
                     LZClose(hSource);
                     break;
                  }

                  // copy source to dest, decompressing if necessary
                  if (hSource != -1 && hDest != -1)
                  {
                     CopyLZFile(hSource, hDest);

                     // close both source and dest
                     LZClose(hSource);
                     LZClose(hDest);
                  }
               }
               // free memory allocated by LZStart
               LZDone();
            }
               break;

            case IDM_ABOUT:  // About box
               lpProc = MakeProcInstance(About, ghInst);
               DialogBox(ghInst, "AboutBox", hWnd, lpProc);
               FreeProcInstance(lpProc);
               break;
         }
         break;

      case WM_DESTROY:
         PostQuitMessage(0);
         break;
   }
   return (DefWindowProc(hWnd, msg, wParam, lParam));
}   //*** MainWndProc()

//*************************************************************
//
//  About()
//
//  Purpose:
//            the About dialog box procedure
//
//
//  Parameters:
//      HWND hDlg
//      unsigned msg
//      WORD wParam
//      LONG lParam
//      
//
//  Return: (BOOL FAR PASCAL)  TRUE if message handled, FALSE otherwise
//
//
//  Comments:
//
//
//  History:    Date       Author     Comment
//              12/12/91   EMB        Created
//
//*************************************************************

BOOL FAR PASCAL About (HWND hDlg, unsigned msg, WORD wParam, LONG lParam)
{
   switch (msg)
   {
      case WM_INITDIALOG:
         return (TRUE);

      case WM_COMMAND:
         if (wParam == IDOK || wParam == IDCANCEL)
         {
            EndDialog(hDlg, TRUE);
            return (TRUE);
         }
         break;
   }
   return (FALSE); /* Didn't process a message    */
}   //*** About()


//*************************************************************
//
//  GetOpenFileHandle
//
//  Purpose:
//    Puts up an Open common dialog.  Opens the
//    file the user specifies with LZOpenFile.  
//      
//
//
//  Parameters:
//      HWND hWnd - handle of common dialog's owner
//      LPSTR szOrigName - original file name of opened file
//      LPSTR szTitle - title to be displayed in the common dialog
//      
//
//  Return: (HANDLE FAR) Handle to the opened file.
//
//
//  Comments:
//
//
//  History:    Date       Author          Comment
//               2/16/92   EMB             created
//
//*************************************************************

HANDLE FAR GetOpenFileHandle (HWND hWnd, LPSTR szOrigName, LPSTR szTitle)
{
   HANDLE ret;
   OFSTRUCT ofStructSrc;
   OPENFILENAME ofn;
   char szFile[FILENAMELEN];

   szOrigName[0] = '\0';

   /* Initialize the OPENFILENAME members */
   memset(&ofn, 0, sizeof(OPENFILENAME));
   szFile[0] = '\0';
   ofn.lStructSize = sizeof(OPENFILENAME);
   ofn.hwndOwner = hWnd;
   ofn.hInstance = ghInst;
   ofn.lpstrFilter = (LPSTR)"All Files (*.*)\0*.*\0";
   ofn.lpstrCustomFilter = (LPSTR)NULL;
   ofn.nMaxCustFilter = 0L;
   ofn.nFilterIndex = 0L;
   ofn.lpstrFile = szFile;
   ofn.nMaxFile = FILENAMELEN;
   ofn.lpstrTitle = szTitle;
   ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
   ofn.lpstrDefExt = (LPSTR)NULL;

   // put up the Open common dialog
   if (GetOpenFileName(&ofn))
   {
      // try to open the file
      ret = LZOpenFile(ofn.lpstrFile, (LPOFSTRUCT)&ofStructSrc, OF_READ);

      // get the expanded name of the compressed file
      GetExpandedName(ofn.lpstrFile, szOrigName);

      return (ret);
   }
   else
      return -1;
}


//*************************************************************
//
//  GetSaveFileHandle
//
//  Purpose:
//    Puts up a Save As common dialog.  Tries to open the
//    file the user specifies with LZOpenFile.  
//      
//
//
//  Parameters:
//      HWND hWnd - handle of common dialog's owner
//      LPSTR szTitle - title to be displayed in the common dialog
//      
//
//  Return: (HANDLE FAR) - Handle to the file to be saved to.
//
//
//  Comments:
//
//
//  History:    Date       Author          Comment
//               2/16/92   EMB             Created
//
//*************************************************************

HANDLE FAR GetSaveFileHandle (HWND hWnd, LPSTR szTitle)
{
   OPENFILENAME ofn;
   char szFile[FILENAMELEN];
   OFSTRUCT ofStructSrc;

   /* Initialize the OPENFILENAME members */

   memset(&ofn, 0, sizeof(OPENFILENAME));
   szFile[0] = '\0';
   ofn.lStructSize = sizeof(OPENFILENAME);
   ofn.hwndOwner = hWnd;
   ofn.hInstance    = ghInst;
   ofn.lpstrFilter = (LPSTR)NULL;
   ofn.lpstrCustomFilter = (LPSTR)NULL;
   ofn.nMaxCustFilter = 0L;
   ofn.nFilterIndex = 0L;
   ofn.lpstrFile = szFile;
   ofn.nMaxFile = FILENAMELEN;
   ofn.lpstrTitle = szTitle;
   ofn.Flags = OFN_OVERWRITEPROMPT;

   // put up the Save As common dialog
   if (GetSaveFileName(&ofn))
   {
      // try to open the file
      return (LZOpenFile(ofn.lpstrFile, (LPOFSTRUCT)&ofStructSrc, OF_READWRITE | OF_CREATE));
   }
   else
      return -1;
}
//*** EOF: main.c

0
 
LVL 5

Accepted Solution

by:
bin_huwairib earned 100 total points
ID: 1427029
peterwest,

I will try to explain in breif how you can use these API functions LZOpenFile, LZRead, LZSeek and LZClose to process a compressed file:

1 – LZOpenFile function creates, opens, reopens, or deletes the specified file.
2 – LZRead function reads (at most) the specified number of bytes from a file and copies them into a buffer in the memory.
3 – LZSeek function moves a file pointer a number of bytes from a starting position.
4 – LZClose function closes a file that was opened by using the LZOpenFile function.

Example:

Consider that you have a compress file called “test.tx_” and located in “c:\”.
1 – Create project1.
2 – Add module1 to project1.
3 – Add form1 to project1.
4 – Add command1 to form1.
5 – Add text1 to form1.

=== Add this code to module1 ===
Public Const OFS_MAXPATHNAME = 128

Type OFSTRUCT
        cBytes As Byte
        fFixedDisk As Byte
        nErrCode As Integer
        Reserved1 As Integer
        Reserved2 As Integer
        szPathName(OFS_MAXPATHNAME) As Byte
End Type

Public Const OF_READ = &H0

Declare Function LZOpenFile Lib "lz32.dll" Alias "LZOpenFileA" (ByVal lpszFile As String, lpOf As OFSTRUCT, ByVal style As Long) As Long
Declare Sub LZClose Lib "lz32.dll" (ByVal hfFile As Long)
Declare Function LZRead Lib "lz32.dll" (ByVal hfFile As Long, ByVal lpvBuf As String, ByVal cbread As Long) As Long
Declare Function LZSeek Lib "lz32.dll" (ByVal hfFile As Long, ByVal lOffset As Long, ByVal nOrigin As Long) As Long


=== Add this code to form1 ===
Private Sub Command1_Click()
 Dim myStruc As OFSTRUCT
 Dim hFile As Long
 Dim Result As Long
 Dim Buffer As String
 
 hFile = LZOpenFile("c:\test.tx_", myStruc, OF_READ)
 Buffer = Space(FileLen("c:\test.tx_"))
 Result = LZRead(hFile, Buffer, Len(Buffer))
 Text1.Text = Left(Buffer, Result)
 Call LZClose(hFile)
End Sub


Best Regards
Bin Huwairib

0
 
LVL 2

Author Comment

by:peterwest
ID: 1427030
Thanks - just what I needed.

Pete

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now