SQL Server
SQL Server
Extract Only Newer Files Using UnzipNewer
See more Zip Examples
This example demonstrates how to use the UnzipNewer method to extract only those ZIP entries that are newer than existing files already present on disk.
This is useful for:
- Updating previously extracted ZIP contents
- Avoiding unnecessary overwriting of unchanged files
- Incremental deployment or synchronization scenarios
- Faster extraction when many files are already up-to-date
Suppose the ZIP archive contains:
docs/readme.txt
images/logo.png
data/config.json And suppose the target extraction directory already contains:
c:/temp/app/docs/readme.txt
c:/temp/app/images/logo.png If the ZIP version of docs/readme.txt is newer than the existing file on disk, it will be extracted and overwrite the existing file.
If the existing images/logo.png file is already newer or has the same timestamp, it will not be overwritten.
Files that do not yet exist on disk are extracted normally.
The UnzipNewer method returns the number of files extracted, or -1 if the operation fails.
Chilkat SQL Server Downloads
-- 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
-- Open an existing ZIP archive.
EXEC sp_OAMethod @zip, 'OpenZip', @success OUT, 'qa_data/zips/applicationUpdate.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- ------------------------------------------------------------
-- Extract only ZIP entries that are newer than the
-- corresponding files already existing on disk.
--
-- Existing files that are already up-to-date are skipped.
--
DECLARE @numFilesUnzipped int
EXEC sp_OAMethod @zip, 'UnzipNewer', @numFilesUnzipped OUT, 'c:/temp/app'
IF @numFilesUnzipped < 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
PRINT 'Number of files extracted = ' + @numFilesUnzipped
PRINT ''
-- ------------------------------------------------------------
-- Example behavior:
--
-- ZIP contains:
--
-- docs/readme.txt
-- images/logo.png
-- data/config.json
--
-- Existing filesystem files:
--
-- c:/temp/app/docs/readme.txt
-- c:/temp/app/images/logo.png
--
-- If the ZIP version of docs/readme.txt is newer,
-- it will overwrite the existing file.
--
-- If c:/temp/app/images/logo.png is already newer,
-- it will be skipped.
--
-- data/config.json will be extracted if it does not
-- already exist.
--
EXEC sp_OAMethod @zip, 'CloseZip', NULL
PRINT 'UnzipNewer completed successfully.'
EXEC @hr = sp_OADestroy @zip
END
GO