![]() |
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) Add Text Files to a ZIP Using AddStringSee more Zip ExamplesThis example demonstrates how to use the Each string is converted to bytes using the specified character encoding and stored as a separate file entry within the ZIP archive. This method is useful for dynamically generating small text files such as configuration files, reports, JSON documents, XML, or log files entirely in memory.
-- 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 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, 'stringEntries.zip' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- Add a README text file. DECLARE @readmeText nvarchar(4000) SELECT @readmeText = 'This ZIP archive was created using AddString.' EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'docs/readme.txt', @readmeText, 'utf-8' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- Add a JSON configuration file. DECLARE @jsonText nvarchar(4000) SELECT @jsonText = '{ "server": "example.com", "port": 443 }' EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'config/settings.json', @jsonText, 'utf-8' IF @success = 0 BEGIN EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @zip RETURN END -- Add a small XML document. DECLARE @xmlText nvarchar(4000) SELECT @xmlText = '<root><status>OK</status></root>' EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'xml/status.xml', @xmlText, '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 PRINT 'ZIP archive created successfully.' EXEC @hr = sp_OADestroy @zip END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.