![]() |
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) Extract a Single ZIP Entry While Preserving Its Stored PathSee more Zip Examples This example demonstrates how to use the The example first creates a ZIP archive containing a text file. It then re-opens the ZIP, finds the entry, and extracts it to a base directory. 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 -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) DECLARE @success int SELECT @success = 0 -- ------------------------------------------------------------ -- First create a sample ZIP archive containing a small text file. DECLARE @zip int EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'c:/temp/sampleText.zip' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- Add a small text file from memory. EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'myApp/docs/hello.txt', 'Hello from a ZIP entry!', 'utf-8' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- Write the ZIP archive to disk and close it. EXEC sp_OAMethod @zip, 'WriteZipAndClose', @success OUT IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- ------------------------------------------------------------ -- Now open the ZIP archive we just created. DECLARE @zip2 int EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip2 OUT EXEC sp_OAMethod @zip2, 'OpenZip', @success OUT, 'c:/temp/sampleText.zip' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip2, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @zip2 RETURN END -- Find the entry by its stored ZIP path. DECLARE @entry int EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT EXEC sp_OAMethod @zip2, 'EntryOf', @success OUT, 'myApp/docs/hello.txt', @entry IF @success = 0 BEGIN PRINT 'Entry not found.' EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @zip2 EXEC @hr = sp_OADestroy @entry RETURN END -- Extract this single entry to the base directory: -- -- c:/temp/extracted -- -- Because the entry's stored path is: -- -- myApp/docs/hello.txt -- -- the file will be extracted to: -- -- qa_output/extracted/myApp/docs/hello.txt -- -- The required subdirectories are created automatically. EXEC sp_OAMethod @entry, 'Extract', @success OUT, 'c:/temp/extracted' IF @success = 0 BEGIN EXEC sp_OAGetProperty @entry, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @zip2 EXEC @hr = sp_OADestroy @entry RETURN END EXEC sp_OAMethod @zip2, 'CloseZip', NULL PRINT 'Entry extracted successfully.' EXEC @hr = sp_OADestroy @zip EXEC @hr = sp_OADestroy @zip2 EXEC @hr = sp_OADestroy @entry END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.