Execute process task to Zip files
Hi
I am using an execute process task to zip files. I have a variable named 'File_Path' which contains dynamic name of the file like 'FILE_MMDDYYYY'
After the file is created at the required destination, i placed execute process task so that it can pick the file for that location and zip it .
I am getting error as:
In Executing "C:\Program Files (x86)\7-Zip\7z.exe" "C:\Users\PackageSSIS\20120523140623\file_20120523140623.txt" at "C:\Program Files (x86)\7-Zip", The process exit code was "7" while the expected was "0".
I am using following in execute process task:
In the executable: C:\Program Files (x86)\7-Zip\7z.exe
Working directory : C:\Program Files (x86)\7-Zip
In Expression:
Property Expression
Argument @[User::File_Path]
Please Advice
Thanks
May 23rd, 2012 5:32pm
what is in the @[User::File_Path] when you run?
Thing is as per http://sevenzip.sourceforge.jp/chm/cmdline/exit_codes.htm code 7 means command line error.
Typically these are due to spaces in path as in Program Files. Make sure you surround the path in quiates always.Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2012 5:40pm
You need to review the command-line syntax for 7-zip. Simply providing the name of a text file is insufficient instruction. What is 7-zip to do? Compress that file? Extract it? To/from what archive or folder?
Construct your arguments and test them on a command line. Then place your arguments into the Execute Process Task.
Talk to me now on
May 23rd, 2012 5:43pm
Your argument is wrong. You are not specifying a command or an archive target
SHould be "a archivename " + @[User::File_Path]
Chuck Pedretti | Magenic North Region | magenic.com
Free Windows Admin Tool Kit Click here and download it now
May 23rd, 2012 5:50pm
Alternatively you can use a script task to achieve this - all native .net code and you don;t need anything on the server - just 7zip on the client to open the files if necessary:
Imports System.IO
Imports System.IO.Compression
Public Sub Main()
'*************************************************************************************************
'**** Archive area is always a subfolder of landing zone and called _archive ****
'*************************************************************************************************
Dim SourceFile As String
Dim DestFile As String
Dim DestFileZip As String
'Set result to success in the 1st instance. Only change on failure
Dts.TaskResult = ScriptResults.Success
Try
'Assign variables as the source / destination for the file move
SourceFile = Dts.Variables("User::ArchiveSourceFilePath").Value.ToString + Dts.Variables("User::ArchiveFileName").Value.ToString
DestFile = Dts.Variables("User::ArchiveDestFilePath").Value.ToString + Dts.Variables("User::ArchiveFileName").Value.ToString
'check if the file already exists - shouldn't do but it is possible if re-run is occurring
If File.Exists(DestFile) Then
'If file exists in destination folder, delete it as we want the latest version to be in there
'and move file method will not overwrite existing files
File.Delete(DestFile)
End If
'check if zipped version of the file exists as well
'find the "." in the file name and strip off from that point - replace with ".gz" which is the
'extension used in the compression process
'Requires a -1 as instr returns the position of the "." and we want the characters up to but not
'including the "."
DestFileZip = Left(DestFile, InStr(DestFile, ".") - 1) & ".gz"
If File.Exists(DestFileZip) Then
'If zipped file exists in destination folder, delete it as we want the latest version to be in there
'and creation of new zip file with the same name as an existing one will cause errors
File.Delete(DestFile)
End If
'physically move the file from the landing zone to archive
File.Move(SourceFile, DestFile)
'Compress the file
Compress(DestFile)
'Delete the non compressed file
File.Delete(DestFile)
Catch ex As Exception
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
'*****************************************************************************************************
'**** USES GZip COMPRESSION TO COMPRESS FILES - REQUIRES WINZIP / 7-ZIP TO OPEN BUT DOES ****
'**** NOT REQUIRE 3RD PARTY SOFTWARE ON TEH SERVER - ONLY THE CLIENT ****
'*****************************************************************************************************
Public Sub Compress(ByVal FilePath As String)
Dim UncompressedData As Byte() = System.IO.File.ReadAllBytes(FilePath)
Dim CompressedData As New MemoryStream()
Dim GZipper As New GZipStream(CompressedData, CompressionMode.Compress, True)
GZipper.Write(UncompressedData, 0, UncompressedData.Length)
GZipper.Dispose()
System.IO.File.WriteAllBytes(Left(FilePath, InStr(FilePath, ".") - 1) + ".gz", CompressedData.ToArray)
CompressedData.Dispose()
End Sub
Rgds Geoff
----------------------------------------------------------
Mark as Answer if this resolves your problem or "Vote as Helpful" if you find it helpful.
May 24th, 2012 1:15am
Alternatively you can use a script task to achieve this - all native .net code and you don;t need anything on the server - just 7zip on the client to open the files if necessary:
Imports System.IO
Imports System.IO.Compression
Public Sub Main()
'*************************************************************************************************
'**** Archive area is always a subfolder of landing zone and called _archive ****
'*************************************************************************************************
Dim SourceFile As String
Dim DestFile As String
Dim DestFileZip As String
'Set result to success in the 1st instance. Only change on failure
Dts.TaskResult = ScriptResults.Success
Try
'Assign variables as the source / destination for the file move
SourceFile = Dts.Variables("User::ArchiveSourceFilePath").Value.ToString + Dts.Variables("User::ArchiveFileName").Value.ToString
DestFile = Dts.Variables("User::ArchiveDestFilePath").Value.ToString + Dts.Variables("User::ArchiveFileName").Value.ToString
'check if the file already exists - shouldn't do but it is possible if re-run is occurring
If File.Exists(DestFile) Then
'If file exists in destination folder, delete it as we want the latest version to be in there
'and move file method will not overwrite existing files
File.Delete(DestFile)
End If
'check if zipped version of the file exists as well
'find the "." in the file name and strip off from that point - replace with ".gz" which is the
'extension used in the compression process
'Requires a -1 as instr returns the position of the "." and we want the characters up to but not
'including the "."
DestFileZip = Left(DestFile, InStr(DestFile, ".") - 1) & ".gz"
If File.Exists(DestFileZip) Then
'If zipped file exists in destination folder, delete it as we want the latest version to be in there
'and creation of new zip file with the same name as an existing one will cause errors
File.Delete(DestFile)
End If
'physically move the file from the landing zone to archive
File.Move(SourceFile, DestFile)
'Compress the file
Compress(DestFile)
'Delete the non compressed file
File.Delete(DestFile)
Catch ex As Exception
Dts.TaskResult = ScriptResults.Failure
End Try
End Sub
'*****************************************************************************************************
'**** USES GZip COMPRESSION TO COMPRESS FILES - REQUIRES WINZIP / 7-ZIP TO OPEN BUT DOES ****
'**** NOT REQUIRE 3RD PARTY SOFTWARE ON TEH SERVER - ONLY THE CLIENT ****
'*****************************************************************************************************
Public Sub Compress(ByVal FilePath As String)
Dim UncompressedData As Byte() = System.IO.File.ReadAllBytes(FilePath)
Dim CompressedData As New MemoryStream()
Dim GZipper As New GZipStream(CompressedData, CompressionMode.Compress, True)
GZipper.Write(UncompressedData, 0, UncompressedData.Length)
GZipper.Dispose()
System.IO.File.WriteAllBytes(Left(FilePath, InStr(FilePath, ".") - 1) + ".gz", CompressedData.ToArray)
CompressedData.Dispose()
End Sub
Rgds Geoff
----------------------------------------------------------
Mark as Answer if this resolves your problem or "Vote as Helpful" if you find it helpful.
Free Windows Admin Tool Kit Click here and download it now
May 24th, 2012 1:20am