Microsoft KB Archive/200649

= How To Use LZCopy to Copy Files =

Article ID: 200649

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition

-



This article was previously published under Q200649



SUMMARY
This article demonstrates how to use the LZOpenFile, LZCopy, and LZClose functions from the LZ Expand/Compress library (LZ32.dll). You can use these functions to make a copy of an existing file. The LZCopy function creates a decompressed destination file if the source file is compressed with the Microsoft File Compression Utility (Compress.exe).



Step-by-Step Example
These steps demonstrate how to use the functions from the LZ Expand/Compress library to copy files to and from locations on a local or network drive.  Create a new Standard EXE project. Form1 is created by default. Add two Labels, two TextBoxes, and a CommandButton to Form1. Position Label1 and Label2 above Text1 and Text2, respectively.  Add the following code to the General Declarations section of Form1: Option Explicit

Const OF_CREATE = &H1000 Const OF_READ = &H0 Const OFS_MAXPATHNAME = 128

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

Private Declare Sub LZClose Lib "lz32.dll" _ (ByVal hfFile As Long) Private Declare Function LZCopy Lib "lz32.dll" _ (ByVal hfSource As Long, ByVal hfDest As Long) As Long Private Declare Function LZOpenFile Lib "lz32.dll" Alias "LZOpenFileA" _ (ByVal lpszFile As String, lpOf As OFSTRUCT, ByVal style As Long) _ As Long

Public Sub CopyFile(Source As String, Destination As String) Dim hsource As Long Dim hdest As Long Dim iret As Long Dim OpenStruct As OFSTRUCT

' Open the source and destination files. hsource = LZOpenFile(Source, OpenStruct, OF_READ) hdest = LZOpenFile(Destination, OpenStruct, OF_CREATE) ' Copy the source file to the destination location, and ' decompress the Source file if it was compressed. iret = LZCopy(hsource, hdest) ' Close the files. LZClose hdest LZClose hsource If iret = -1 Then MsgBox "File transfer failed" Else MsgBox "Transfer successful: " _ & Format$(iret, "###,###,###,###") _ & " bytes were transferred." End If End Sub

Private Sub Command1_Click CopyFile Text1.Text, Text2.Text End Sub

Private Sub Form_Load Label1.Caption = "Source Directory and Filename" Label2.Caption = "Target Directory and Filename" Command1.Caption = "Copy" End Sub  Run the project. In the Source TextBox, type the full path and file name of the file that you want to copy. In the Target TextBox, type the full path and file name of the destination file, and then click Copy. In Windows Explorer, confirm that the file copied successfully.</li></ol>

<div class="references_section">