Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compression.ZipFile throws ArgumentException "The path is not of a legal form"

I'm trying to use Powershell v3 and .NET 4.5 to change a string in a file in a folder and then zip up the contents of the folder. I've got the string replace working, but then the zip keeps throwing an ArgumentException on the directory path, even though I'm sure it's correct.

Exception calling "CreateFromDirectory" with "4" argument(s): "The path is not of a legal form."
At line:5 char:4
+    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException

Microsoft article on ZipFile.CreateFromDirectory:
http://msdn.microsoft.com/en-us/library/hh485721(v=vs.110).aspx

The code I'm trying:

function ZipFiles( $zipfilename, $sourcedir )
{
   [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
   $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
   [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir,
        $zipfilename, $compressionLevel, $false)
}

Get-Content public-build\index.html | ForEach-Object { $_ -replace "data-default-api=`"dev`"", "data-default-api=`"test`"" } | Set-Content public-build\index2.html
cp public-build\index2.html public-build\index.html
rm public-build\index2.html
ZipFiles("public-build.zip", "C:\Users\Administrator\Desktop\public-build")

I have tried changing "C:\Users\Administrator\Desktop\public-build" to: "C:\Users\Administrator\Desktop\public-build\"
"C:\\Users\\Administrator\\Desktop\\public-build"
"C:\\Users\\Administrator\\Desktop\\public-build\\"
"public-build"
"public-build\"
".\public-build"
".\public-build\"
All throw the same error. I have also tried with a foldername of just "publicbuild", in case it was the hyphen, but still got the same error.

I'm rather stumped. All I want to do is zip up the folder. Hopefully someone will point out some obvious mistake I'm making, but otherwise I also welcome any alternate approaches. I'd prefer not to install third party tools, but may have to resort to it if there is no other solution.

like image 480
Andrew Davison Avatar asked Jan 06 '14 11:01

Andrew Davison


1 Answers

I think the problem is in the way you're providing arguments when you call the function. In Powershell arguments are provided as space separated values, it doesn't use the () syntax.

ZipFiles "public-build.zip" "C:\Users\Administrator\Desktop\public-build"
like image 114
mjolinor Avatar answered Sep 28 '22 00:09

mjolinor