Create and populate a Zip file

  • I have a small program that runs every day from my client machine that creates a new zip folder on a shared drive, then copies the contents of another folder (and its subfolders) into that zip folder for backup. The backed-up files are around 50 MB total before zipping, and contain an ACCDB, ACCDE, and DDL file among a few others.

    I would rather my MS Server 2008 do this job, since it's already there and running my SQL Server instance. I'm thinking there must be a way to do this using T-SQL in a daily scheduled job. Any resources?

    Jim

  • Jim

    If you have xp_cmdshell enabled, you can use it to run the command line for your zip utility. Otherwise, you could consider using SSIS to do the same. Another alternative is to run it from a Windows scheduled task, bypassing SQL Server altogether.

    John

  • here's an xp_cmdshell example, like John mentioned above.

    the list of files, isntead of being a single file, can be a space delimited set uf multiple files.

    --http://www.7-zip.org/download.html

    --http://downloads.sourceforge.net/sevenzip/7za920.zip

    --xp_cmdshell is limited to ONE set of double quotes.

    --one of the paths needs to not contain spaces!

    --see 2003 MS KB http://support.microsoft.com/kb/266224

    DECLARE @results TABLE(results varchar(255))

    declare @command varchar(2000)

    --zip one file

    SET @command =

    '"C:\DataFiles\7zip_CommandLine_7za465\' --path to 7za command line utility note the dbl quotes for long file names!

    + '7za.exe"' --the exe: i'm using in the command line utility.

    + ' a ' --the Add command: add to zip file:

    + 'C:\DataFiles\' --path for zip

    + 'myZipFile.zip' --zip file name, note via xp_cmdshell only one pair of dbl quotes allowed names!

    + ' ' --whitespace between zip file and file to add

    + 'C:\DataFiles\' --path for the files to add

    + 'SandBox_2011-07-25.bak' --the file

    + ' -y' --suppress any dialogs by answering yes to any and all prompts

    print @command

    --"C:\DataFiles\7zip_CommandLine_7za465\7za.exe" a C:\DataFiles\myZipFile.zip C:\DataFiles\SandBox_2011-07-25.bak -y

    insert into @results

    exec xp_cmdshell @command

    select * from @results

    /*

    NULL

    7-Zip (A) 4.65 Copyright (c) 1999-2009 Igor Pavlov 2009-02-03

    NULL

    Scanning

    NULL

    Updating archive C:\DataFiles\myZipFile.zip

    NULL

    Compressing SandBox_2011-07-25.bak

    NULL

    Everything is Ok

    NULL

    */

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell, I continue to struggle with this. I downloaded zip-7, and wrote a vbs script to call it. Problem is, both the source and target paths have spaces in the pathnames. In any shell situation, I can't seem to use two sets of double quotes. One set gets eaten up. Am I doing it wrong?

    Option Explicit

    ' Need to create a full path following the Target Location:

    ' Higher Level Folder: "YYYY-MMM_Backup"

    ' File Name "YY-MM-DD.Zip"

    ' Backing up the "CPAS folder and all its subfolders"

    Dim YYYY

    Dim YY

    Dim MMM

    Dim MM

    Dim DD

    Dim strZipInstruction

    Dim strZipProgLoc

    Dim strTargetLoc

    Dim strSourceLoc

    Dim FileNameZip

    Dim strDate

    Dim oFSO

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    YYYY = Cstr(Year(Date))

    YY = Right(YYYY,2)

    MM = FormatNumber((Month(Date)),0,-1)

    MMM = Left(MonthName(Month(Date)),3)

    DD = FormatNumber(Day(Date),0,-1)

    strTargetLoc = "H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"

    If Not oFSO.FolderExists(strTargetLoc) Then

    oFSO.CreateFolder strTargetLoc

    End If

    strTargetLoc = """H:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"""

    strSourceLoc = "H:\mtv\secure\Construction\Access\CPAS\"

    strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "

    strDate = YY & "-" & MM & "-" & DD

    FileNameZip = strDate & ".zip"

    strZipInstruction=strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc

    MsgBox strZipInstruction

    Jim

  • Jim the single set of double quotes is an xp_cmdshell thing;there's only the classic extra-step work arounds.

    the work around is to either use a variable, or create it in a non-space-containing folder,and simply move it after words....c:\Temp or %appdata%, for example.

    so create the zip in %appdata%\Zipfilename.zip, and then move %appdata%\Zipfilename.zip to the final folder that has spaces.

    if the move is on the same drive it's super quick, since it's just updating the allocaiton tables, right?

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Jim

    A vbs script isn't a SQL Server solution, so it's a little outside the scope of these forums. I think it would be simpler to create a batch file, or use the xp_cmdshell solution described by Lowell. If you're having trouble escaping your double quotes in your vbs script, this discussion has a couple of suggestions.

    John

  • jim if you print the variable results, you'll see there's a quote in the wrong place.

    "C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\"12-10-23.zip F:\mtv\secure\Construction\Access\CPAS

    i've corrected your vbs code below to build the path correctly:

    --"C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe" a "F:\mtv\secure\Construction\Access\All Database Backup\2012-Oct_Backup\12-10-23.zip" "F:\mtv\secure\Construction\Access\CPAS\"

    ' Need to create a full path following the Target Location:

    ' Higher Level Folder: "YYYY-MMM_Backup"

    ' File Name "YY-MM-DD.Zip"

    ' Backing up the "CPAS folder and all its subfolders"

    Dim YYYY

    Dim YY

    Dim MMM

    Dim MM

    Dim DD

    Dim strZipInstruction

    Dim strZipProgLoc

    Dim strTargetLoc

    Dim strSourceLoc

    Dim FileNameZip

    Dim strDate

    Dim oFSO

    Set oFSO = CreateObject("Scripting.FileSystemObject")

    YYYY = CStr(Year(Date))

    YY = Right(YYYY, 2)

    MM = FormatNumber((Month(Date)), 0, -1)

    MMM = Left(MonthName(Month(Date)), 3)

    DD = FormatNumber(Day(Date), 0, -1)

    strTargetLoc = "M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"

    If Not oFSO.FolderExists(strTargetLoc) Then

    'oFSO.CreateFolder strTargetLoc

    End If

    strTargetLoc = """M:\mtv\secure\Construction\Access\All Database Backup\" & YYYY & "-" & MMM & "_Backup\"

    strSourceLoc = """M:\mtv\secure\Construction\Access\CPAS\"""

    strZipProgLoc = """C:\Documents and Settings\jamesshaffer\My Documents\7-Zip\7za.exe"" a "

    strDate = YY & "-" & MM & "-" & DD

    FileNameZip = strDate & ".zip"""

    strZipInstruction = strZipProgLoc & strTargetLoc & FileNameZip & " " & strSourceLoc

    'Debug.Print strZipInstruction

    MsgBox strZipInstruction

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • This problem could probably be more "cleanly" solved with a CLR object.

    Happy to help you knock one up if you'd like to go that route.

  • OTF (10/23/2012)


    This problem could probably be more "cleanly" solved with a CLR object.

    Happy to help you knock one up if you'd like to go that route.

    That would be a valuable contribution; i'd love to see it!

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • I'd love to have that learning experience.

    VBS is very unsatisfactory. No editor, No debugger. I wrote the original program in VBA and deployed it as a standalone db (with no tables...) and set it up as a daily scheduled task. I've had lots of authentication issues that I think are now behind me, so that approach will likely work now. We'll see tonight....

    So, if you could show me how to implement this as a CLR object, I'd love to learn it. Thanks.

    Jim

  • JimS-Indy (10/23/2012)


    I'd love to have that learning experience.

    VBS is very unsatisfactory. No editor, No debugger. I wrote the original program in VBA and deployed it as a standalone db (with no tables...) and set it up as a daily scheduled task. I've had lots of authentication issues that I think are now behind me, so that approach will likely work now. We'll see tonight....

    So, if you could show me how to implement this as a CLR object, I'd love to learn it. Thanks.

    Apologies for the late come back. I had to find a little time to do this.

    I was half way through a CLR Solution for this when it occured to me that a Powershell Solution will be even

    simpler.

    Create a job with a Job Step of Type "Powershell" pasting the code below into the little editor:

    [string]$pathToZipExe = "C:\Program Files\7-zip\7z.exe";

    [Array]$arguments = "a", "-tzip", "[archive you wish to create].zip", "[Folder you wish to zip]", "-r";

    & $pathToZipExe $arguments;

    Substitute the placeholders as required and that should be it.

    Let me know if it works for you.

    Thanks for the encouragement Lowell as I'm just learning myself.

  • 7-zip works very nicely in an execute process task in ssis - we rely on it every day.

    For testing purposes make sure you install it in the same drive and directory on your pc as on the live and test servers you deploy the ssis package to e.g. c:\7zip on all.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply