![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Understanding ZIP Entry Types and the ZIP Object LifecycleSee more Zip Examples
This example demonstrates how the The example shows:
The example also demonstrates the meaning of the different
This example is useful for understanding how the
Note: This example requires Chilkat v11.0.0 or greater.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @success int SELECT @success = 0 SELECT @success = 0 DECLARE @zip int EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @zipPath nvarchar(4000) SELECT @zipPath = 'c:/temp/out.zip' -- Start a new Zip object and set the output path. -- This does not create the .zip file yet. It only initializes the Zip object -- and records the filename to be used later when WriteZip is called. EXEC sp_OAMethod @zip, 'NewZip', @success OUT, @zipPath -- Add references to files in the local filesystem. -- AppendFilesEx does not read, compress, or store the file contents at this point. -- It only adds file references to the Zip object. The referenced files are -- read and compressed later, when WriteZip is called. DECLARE @recurse int SELECT @recurse = 1 DECLARE @saveExtraPath int SELECT @saveExtraPath = 0 DECLARE @archiveOnly int SELECT @archiveOnly = 0 DECLARE @includeHidden int SELECT @includeHidden = 1 DECLARE @includeSystem int SELECT @includeSystem = 0 EXEC sp_OAMethod @zip, 'AppendFilesEx', @success OUT, 'c:/temp/files_to_zip', @recurse, @saveExtraPath, @archiveOnly, @includeHidden, @includeSystem IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- We can inspect the entries that have been added to the Zip object. -- At this stage, the entries are still references to files in the local filesystem. DECLARE @entry int EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT DECLARE @count int EXEC sp_OAGetProperty @zip, 'NumEntries', @count OUT DECLARE @i int SELECT @i = 0 WHILE @i < @count BEGIN EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT PRINT @sTmp0 + ' entryType = ' + @iTmp0 SELECT @i = @i + 1 END PRINT '----' -- Sample output for the above loop: -- -- hamlet.xml entryType = 1 -- hello.pdf entryType = 1 -- ... -- ... -- -- entryType = 1 means "File Entry". -- A File Entry is a reference to a file in the local filesystem. -- The file data has not yet been read into the Zip object. -- -- Possible entryType values: -- -- 0 - Mapped Entry: -- An entry that already exists in an open .zip file. -- -- 1 - File Entry: -- A file in the local filesystem that has been referenced, but not yet -- read or compressed. These entries are added by methods such as -- AppendFiles, AppendFilesEx, and AddFile. -- -- 2 - Data Entry: -- An entry containing uncompressed data already held in memory. -- These entries are added by methods such as AddData, AddString, -- AddSb, AddBd, and AddEncoded. -- -- 3 - Null Entry: -- An entry that no longer exists in the .zip. -- -- 4 - New Directory Entry: -- A directory entry added by calling AddEmpty. -- Additional entries can still be added before the .zip is written. -- This entry is created from an in-memory string, so it is a Data Entry -- rather than a File Entry. EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'helloWorld.txt', 'Hello World', 'utf-8' -- Inspect the Zip object again before writing. -- The file references are still entryType = 1. -- The in-memory string entry is entryType = 2. EXEC sp_OAGetProperty @zip, 'NumEntries', @count OUT SELECT @i = 0 WHILE @i < @count BEGIN EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT PRINT @sTmp0 + ' entryType = ' + @iTmp0 SELECT @i = @i + 1 END PRINT '----' -- Sample output: -- -- hamlet.xml entryType = 1 -- hello.pdf entryType = 1 -- ... -- ... -- helloWorld.txt entryType = 2 -- -- helloWorld.txt has entryType = 2 because its contents came from memory. -- It is not a reference to a file in the local filesystem. -- ---------------------------------------------------------------------------- -- Write the Zip object to the .zip file. -- -- During this call, Chilkat reads the referenced files from the local filesystem, -- compresses the data as needed, and writes the final .zip archive. EXEC sp_OASetProperty @zip, 'FileName', @zipPath EXEC sp_OAMethod @zip, 'WriteZip', @success OUT IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @entry RETURN END -- After WriteZip returns successfully, the .zip file is written and remains -- open in the Zip object. The entries now correspond to entries in the -- currently open .zip archive. EXEC sp_OAGetProperty @zip, 'NumEntries', @count OUT SELECT @i = 0 WHILE @i < @count BEGIN EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT PRINT @sTmp0 + ' entryType = ' + @iTmp0 SELECT @i = @i + 1 END PRINT '----' -- Sample output: -- -- hamlet.xml entryType = 0 -- hello.pdf entryType = 0 -- ... -- ... -- helloWorld.txt entryType = 0 -- -- entryType = 0 means "Mapped Entry". -- A Mapped Entry is an entry that exists in an open .zip file. -- ---------------------------------------------------------------------------- -- The .zip remains open after WriteZip, so additional entries can still be added. -- -- This adds another in-memory string entry to the currently open Zip object. EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'helloWorld2.txt', 'Hello World 2', 'utf-8' -- Write the .zip again to include the newly added entry. EXEC sp_OAMethod @zip, 'WriteZip', @success OUT IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @entry RETURN END -- Inspect the entries one final time. -- After the second WriteZip, the new entry is also mapped to the open .zip file. EXEC sp_OAGetProperty @zip, 'NumEntries', @count OUT SELECT @i = 0 WHILE @i < @count BEGIN EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT PRINT @sTmp0 + ' entryType = ' + @iTmp0 SELECT @i = @i + 1 END PRINT '----' -- Sample output: -- -- hamlet.xml entryType = 0 -- hello.pdf entryType = 0 -- ... -- ... -- helloWorld.txt entryType = 0 -- helloWorld2.txt entryType = 0 -- Close the open .zip archive and clear the Zip object. EXEC sp_OAMethod @zip, 'CloseZip', NULL PRINT 'Success' EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @entry END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.