Skip to content

Commit

Permalink
Revert "Adding Relative not yet complete."
Browse files Browse the repository at this point in the history
This reverts commit 206008f.
  • Loading branch information
guitarrrapc committed Oct 9, 2013
1 parent 206008f commit 3e8b009
Showing 1 changed file with 54 additions and 186 deletions.
240 changes: 54 additions & 186 deletions PS-Zip/PS-Zip.psm1
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function New-ZipCompress{
#Requires -Version 3.0

function New-ZipCompress{

[CmdletBinding(DefaultParameterSetName="safe")]
param(
Expand All @@ -22,24 +24,18 @@
mandatory = 0,
position = 2)]
[switch]
$relative,

[parameter(
mandatory = 0,
position = 3)]
[switch]
$quiet,

[parameter(
mandatory = 0,
position = 4,
position = 3,
ParameterSetName="safe")]
[switch]
$safe,

[parameter(
mandatory = 0,
position = 4,
position = 3,
ParameterSetName="force")]
[switch]
$force
Expand Down Expand Up @@ -85,7 +81,6 @@
# set zip extension
$zipExtension = ".zip"

# absolute destination
Write-Debug ("set desktop as destination path destination {0} is null" -f $destination)
if ([string]::IsNullOrWhiteSpace($destination))
{
Expand Down Expand Up @@ -137,199 +132,73 @@
Write-Verbose ("zipExtension : {0}" -f $zipExtension)

$destination = Join-Path $desktop ($filename + $zipExtension)
}
}
}
elseif ($destination.EndsWith(".") -or $destination.EndsWith("\") -or $destination.EndsWith("/") -or ([System.IO.Path]::GetExtension($destination) -eq ""))
{
$destinationpath = $destination

if ($file.PSISContainer -and ($file.count -eq 1))
{
Write-Verbose "Detected as Directory"

if ($file.FullName -eq $file.Root)
{
$filename = $file.PSDrive.Name
}
else
{
# remove \ or / on last letter of source
$fullpath = Join-Path (Split-Path -Path $file -Parent) (Split-Path -Path $file -Leaf)
$filename = [System.IO.Path]::GetFileName($fullpath)
}

Write-Verbose ("destinationpath : {0}" -f $destinationpath)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)

$destination = Join-Path $destinationpath ($filename + $zipExtension)
}
elseif ($file.PSISContainer -and ($file.count -gt 1) -and ($source[-1] -eq "*"))
{
Write-Verbose "Detected as source which use * without extension"
Write-Verbose "create zip from parent directory when last letter of source was wildcard *"

$filename = ([System.IO.Path]::GetFileNameWithoutExtension($file.FullName))

Write-Verbose ("destinationpath : {0}" -f $destinationpath)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)

$destination = Join-Path $destinationpath ($filename + $zipExtension)
}
else
{
Write-Verbose "Detected as File"

# use first file name as zip name
$filename = ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname)))

Write-Verbose ("SourcePath : {0}" -f $sourcepath)
Write-Verbose ("GetFileName : {0}" -f ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname))))
Write-Verbose ("zipExtension : {0}" -f $zipExtension)

$destination = Join-Path $sourcepath ($filename + $zipExtension)
}
}
elseif (([System.IO.Path]::GetExtension($destination) -ne "") -and ([System.IO.Path]::GetExtension($destination) -ne $zipExtension))
Write-Debug "check destination is input as .zip"
if (-not($destination.EndsWith($zipExtension)))
{
throw ("destination [{0}] extension {1} was not zip extension {2}" -f $destination, [System.IO.Path]::GetExtension($destination), $zipExtension)
}
else
{
Resolve-Path $destination
throw ("destination parameter value [{0}] not end with extension {1}" -f $destination, $zipExtension)
}

# relative destination
if ($relative)
Write-Debug "check destination is already exist, CreateFromDirectory Method will fail with same name of destination file."
if (Test-Path $destination)
{
$tmp = $env:TMP

if ($file.PSISContainer -and ($file.count -eq 1))
if ($safe)
{
Write-Verbose "Detected as Directory"

if ($file.FullName -eq $file.Root)
{
$filename = $file.PSDrive.Name
}
else
{
# remove \ or / on last letter of source
$fullpath = Join-Path (Split-Path -Path $file -Parent) (Split-Path -Path $file -Leaf)
$filename = [System.IO.Path]::GetFileName($fullpath)
}
Write-Debug "safe output zip file to new destination path, avoiding destination zip name conflict."

Write-Verbose ("Tmp : {0}" -f $tmp)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)
# show warning for same destination exist.
Write-Verbose ("Detected destination name {0} is already exist." -f $destination)

$destination = Join-Path $tmp ($filename + $zipExtension)
}
elseif ($file.PSISContainer -and ($file.count -gt 1) -and ($source[-1] -eq "*"))
{
Write-Verbose "Detected as source which use * without extension"
Write-Verbose "create zip from parent directory when last letter of source was wildcard *"

$filename = ([System.IO.Path]::GetFileNameWithoutExtension($file.FullName))
$olddestination = $destination

Write-Verbose ("Tmp : {0}" -f $tmp)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)
# get current destination information
$destinationRoot = [System.IO.Path]::GetDirectoryName($destination)
$destinationfile = [System.IO.Path]::GetFileNameWithoutExtension($destination)
$destinationExtension = [System.IO.Path]::GetExtension($destination)

$destination = Join-Path $tmp ($filename + $zipExtension)
# renew destination name with (2)...(x) until no more same name catch.
$count = 2
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
while (Test-Path $destination)
{
++$count
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
}

# show warning as destination name had been changed due to escape error.
Write-Warning ("Safe old deistination {0} change to new name {1}" -f $olddestination, $destination)
}
else
{
Write-Verbose "Detected as File"

# use first file name as zip name
$filename = ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname)))

Write-Verbose ("Tmp : {0}" -f $tmp)
Write-Verbose ("GetFileName : {0}" -f ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname))))
Write-Verbose ("zipExtension : {0}" -f $zipExtension)

$destination = Join-Path $tmp ($filename + $zipExtension)
}
}

Write-Debug "check destination is already exist, CreateFromDirectory Method will fail with same name of destination file."
# swap destination for temporary
if ($relative)
{
$newdestination = $ExecutionContext.SessionState.Path.NormalizeRelativePath($destination, $source)
Push-Location (Split-Path -Path $source -Parent)
$currentdestination = $destination
$destination = $newdestination
}

# execute check
if (Test-Path $destination)
{
if ($safe)
if($force)
{
Write-Debug "safe output zip file to new destination path, avoiding destination zip name conflict."

# show warning for same destination exist.
Write-Verbose ("Detected destination name {0} is already exist." -f $destination)

$olddestination = $destination

# get current destination information
$destinationRoot = [System.IO.Path]::GetDirectoryName($destination)
$destinationfile = [System.IO.Path]::GetFileNameWithoutExtension($destination)
$destinationExtension = [System.IO.Path]::GetExtension($destination)

# renew destination name with (2)...(x) until no more same name catch.
$count = 2
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
while (Test-Path $destination)
{
++$count
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
}

# show warning as destination name had been changed due to escape error.
Write-Warning ("Safe old deistination {0} change to new name {1}" -f $olddestination, $destination)
Write-Warning ("force replacing old zip file {0}" -f $destination)
Remove-Item -Path $destination -Force
}
else
{
if($force)
{
Write-Warning ("force replacing old zip file {0}" -f $destination)
Remove-Item -Path $destination -Force
}
else
{
Remove-Item -Path $destination -Confirm
}

if (Test-Path $destination)
{
Write-Warning "Cancelled removing item. Quit cmdlet execution."
return
}
Remove-Item -Path $destination -Confirm
}
}
else
{
Write-Debug ("Destination not found. Check parent folder for destination {0} is exist." -f $destination)
$parentpath = Split-Path $destination -Parent

if (-not(Test-Path $parentpath))
if (Test-Path $destination)
{
Write-Warning ("Parent folder {0} not found. Creating path." -f $parentpath)
New-Item -Path $parentpath -ItemType Directory -Force > $null
Write-Warning "Cancelled removing item. Quit cmdlet execution."
return
}
}

# back destination
if ($relative)
}
else
{
$destination = $currentdestination
Pop-Location
Write-Debug ("Destination not found. Check parent folder for destination {0} is exist." -f $destination)
$parentpath = Split-Path $destination -Parent

if (-not(Test-Path $parentpath))
{
Write-Warning ("Parent folder {0} not found. Creating path." -f $parentpath)
New-Item -Path $parentpath -ItemType Directory -Force
}
}


Expand Down Expand Up @@ -364,6 +233,7 @@
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.fullname,$destination,$compressionLevel,$includeBaseDirectory)
Get-Item $destination
}
}
catch
Expand Down Expand Up @@ -395,6 +265,7 @@
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.FullName,$destination,$compressionLevel,$includeBaseDirectory)
Get-Item $destination
}
}
catch
Expand Down Expand Up @@ -430,6 +301,11 @@
{
$?
}
else
{
Get-Item $destination
}

}
catch
{
Expand All @@ -442,14 +318,6 @@
$destzip.Dispose()
}
}

if ($relative)
{
Move-Item -Path $destination -Destination $newdestination -Force
}

# Show result
Get-Item $destination
}

end
Expand Down

0 comments on commit 3e8b009

Please sign in to comment.