Results 1 to 16 of 16

Thread: [RESOLVED] Using T-SQL to compress file using Windows compressing utility

Threaded View

  1. #7

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Using T-SQL to compress file using Windows compressing utility

    Okay, here is the finished code which will allow us to compress a file like in Windows. I have made this so that I can compress my database back-up files to conserve hard disk space.
    Code:
    CREATE PROCEDURE CompressFile 
                    @ZipFile   VARCHAR(255), 
                    @FileToZip VARCHAR(255) 
    AS 
      --author: dee-u of www.vbforums.com, 08-28-2009 
      DECLARE  @hr           INT, 
               @folderObject INT, 
               @shellObject  INT, 
               @src          VARCHAR(255), 
               @desc         VARCHAR(255), 
               @command      VARCHAR(255), 
               @password     VARCHAR(255), 
               @username     VARCHAR(255) 
       
      SET @username = 'username'    
      SET @password = 'password' 
       
      --Create table to save dummy text to create zip file 
      CREATE TABLE ##DummyTable ( [DummyColumn] [VARCHAR](255)) 
       
      --header of a zip file 
      DECLARE  @zipHeader VARCHAR(22)    
      SET @zipHeader = CHAR(80) + CHAR(75) + CHAR(5) + CHAR(6) + REPLICATE(CHAR(0),18) 
       
      --insert zip header 
      INSERT INTO ##DummyTable (DummyColumn) VALUES (@zipHeader) 
       
      --save/create target zip 
      SET @command = 'bcp "..##DummyTable" out "' + @ZipFile + '" -c -S "(local)" -U "' + @username + '" -P "' + @password + '"'    
      EXEC MASTER..xp_cmdshell @command 
       
      --Drop used temporary table 
      DROP TABLE ##DummyTable 
       
      --get shell object 
      EXEC @hr = sp_OACreate 
        'Shell.Application' , 
        @shellObject OUT 
       
      IF @hr <> 0 
        BEGIN 
          EXEC sp_OAGetErrorInfo @shellObject , @src OUT , @desc OUT 
           
          SELECT hr = convert(VARBINARY(4),@hr), 
                 Source = @src, 
                 DESCRIPTION = @desc 
           
          RETURN 
        END 
       
      --get folder 
      SET @command = 'NameSpace("' + @ZipFile + '")'    
      EXEC @hr = sp_OAMethod @shellObject , @command , @folderObject OUT    
      IF @hr <> 0 
        BEGIN 
          EXEC sp_OAGetErrorInfo @shellObject , @src OUT , @desc OUT 
           
          SELECT hr = convert(VARBINARY(4),@hr), 
                 Source = @src, 
                 DESCRIPTION = @desc 
           
          RETURN 
        END 
       
      --copy file to zip file 
      SET @command = 'CopyHere("' + @FileToZip + '")'    
      EXEC @hr = sp_OAMethod @folderObject , @command 
      IF @hr <> 0 
        BEGIN 
          EXEC sp_OAGetErrorInfo @folderObject , @src OUT , @desc OUT 
           
          SELECT hr = convert(VARBINARY(4),@hr), 
                 Source = @src, 
                 DESCRIPTION = @desc 
           
          RETURN 
        END 
       
      --Destroy the objects used. 
      EXEC sp_OADestroy @shellObject    
      EXEC sp_OADestroy @folderObject 
    
    GO
    Sample usage:
    Code:
    EXEC CompressFile  'C:\test.zip',  'C:\test.jpg'
    Last edited by dee-u; Sep 18th, 2009 at 08:54 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width