Differences

This shows you the differences between the selected revisions of the page.

library_example_zip_and_upload 2016-07-29 library_example_zip_and_upload 2023-12-13 (current)
Line 3: Line 3:
The following script uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it. The following script uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it.
-The script is distributed in WinSCP installer as a [[extension|WinSCP extension]]. &beta+The script is distributed in WinSCP installer as a [[extension|WinSCP extension]].
-To use a different archive format than ZIP, you can install [[http://www.7-zip.org/|7-Zip]], add [[#options|configure the extension]] to use it.+To use a different archive format than ZIP, you can install [[https://www.7-zip.org/|7-Zip]], add [[#options|configure the extension]] to use it. 
 + 
 +~~AD~~
To run the script manually use: To run the script manually use:
<code> <code>
-powershell.exe -File "ZipUpload.ps1" -remotePath "/remote/path" -archiveName "archive.zip" file1.dat file2.dat+powershell.exe -File "ZipUpload.ps1" -sessionUrl "sftp://username:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/" -remotePath "/remote/path" -archiveName "archive.zip" file1.dat file2.dat
</code> </code>
 +
 +For opposite functionality, use extension [[extension_archive_and_download|*]].
<code powershell - ZipUpload.ps1> <code powershell - ZipUpload.ps1>
# @name        &ZIP and Upload... # @name        &ZIP and Upload...
-# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%" -sessionUrl "!S" -remotePath "!/" -archiveName "%ArchiveName%" -pause -sessionLogPath "%SessionLogPath%" %Use7zip% -path7zip "%Path7zip%" -archive7zip %Archive7zip% !&+# @command      powershell.exe -ExecutionPolicy Bypass -File "%EXTENSION_PATH%"
 +#                  -sessionUrl "!E" -remotePath "!/" -archiveName "%ArchiveName%"
 +#                  -refresh -pause -sessionLogPath "%SessionLogPath%"
 +#                  %Use7zip% -path7zip "%Path7zip%" -archive7zip %Archive7zip% !&
# @description  Packs the selected files to a ZIP archive and uploads it # @description  Packs the selected files to a ZIP archive and uploads it
# @flag        ApplyToDirectories # @flag        ApplyToDirectories
-# @version      3+# @version      9
# @homepage    ~~SELF~~ # @homepage    ~~SELF~~
-# @require      WinSCP 5.8.4+# @require      WinSCP 5.16
# @require      .NET 4.5 # @require      .NET 4.5
# @option      ArchiveName -run textbox "&Archive name:" "archive" # @option      ArchiveName -run textbox "&Archive name:" "archive"
# @option      - -config -run group "7-zip" # @option      - -config -run group "7-zip"
# @option        Use7zip -config -run checkbox "Use &7-zip" "" -use7zip # @option        Use7zip -config -run checkbox "Use &7-zip" "" -use7zip
-# @option        Archive7zip -config -run dropdownlist "&Archive type (with 7-zip):" zip zip 7z xz gzip bzip2 tar +# @option        Archive7zip -config -run dropdownlist "Archive &type (with 7-zip):"
-# @option        Path7zip -config file "7-zip &path (7z.exe/7za.exe):" "C:\Program Files\7-Zip\7z.exe"+#                    zip zip 7z xz gzip bzip2 tar 
 +# @option        Path7zip -config file "7-zip &path (7z.exe/7za.exe):"
 +#                    "C:\Program Files\7-Zip\7z.exe"
# @option      - -config group "Logging" # @option      - -config group "Logging"
# @option        SessionLogPath -config sessionlogfile # @option        SessionLogPath -config sessionlogfile
Line 32: Line 41:
param ( param (
-    # Use Generate URL function to obtain a value for -sessionUrl parameter. +    # Use Generate Session URL function to obtain a value for -sessionUrl parameter. 
-    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", +    $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", 
-    [Parameter(Mandatory)]+    [Parameter(Mandatory = $True)]
    $remotePath,     $remotePath,
    [Switch]     [Switch]
-    $pause = $False,+    $pause,
    [Switch]     [Switch]
-    $use7Zip = $False,+    $refresh, 
 +   [Switch] 
 + ···$use7Zip,
    # The 7z.exe can be replaced with portable 7za.exe     # The 7z.exe can be replaced with portable 7za.exe
    $path7zip = "C:\Program Files\7-Zip\7z.exe",     $path7zip = "C:\Program Files\7-Zip\7z.exe",
    $archive7zip = "zip",     $archive7zip = "zip",
-    [Parameter(Mandatory)]+    [Parameter(Mandatory = $True)]
    $archiveName,     $archiveName,
    $sessionLogPath = $Null,     $sessionLogPath = $Null,
-    [Parameter(Mandatory, ValueFromRemainingArguments, Position=0)]+    [Parameter(Mandatory = $True, ValueFromRemainingArguments = $True, Position·=·0)]
    $localPaths     $localPaths
) )
Line 61: Line 72:
    }     }
-    Write-Host ("Archiving {0} files to archive {1}..." -f $localPaths.Count, $archiveName)+    Write-Host "Archiving $($localPaths.Count) files to archive $archiveName..."
       
    $archivePath = Join-Path ([System.IO.Path]::GetTempPath()) $archiveName     $archivePath = Join-Path ([System.IO.Path]::GetTempPath()) $archiveName
Line 83: Line 94:
    else     else
    {     {
 +        if ($PSVersionTable.PSVersion.Major -lt 3)
 +        {
 +            throw ("PowerShell 3.0 and newer is required." +
 +                  "Please, upgrade PowerShell. Or try using the 7-zip mode instead.")
 +        }
 +
        Add-Type -AssemblyName "System.IO.Compression"         Add-Type -AssemblyName "System.IO.Compression"
        Add-Type -AssemblyName "System.IO.Compression.FileSystem"         Add-Type -AssemblyName "System.IO.Compression.FileSystem"
-        $zip = [System.IO.Compression.ZipFile]::Open($archivePath, [System.IO.Compression.ZipArchiveMode]::Create)+        $zip = 
 + ···········[System.IO.Compression.ZipFile]::Open( 
 +················$archivePath, [System.IO.Compression.ZipArchiveMode]::Create)
        # Replace with Compress-Archive once PowerShell 5.0 is widespread         # Replace with Compress-Archive once PowerShell 5.0 is widespread
Line 93: Line 112:
        {         {
            $parentPath = Split-Path -Parent (Resolve-Path $localPath)             $parentPath = Split-Path -Parent (Resolve-Path $localPath)
 +            if ($parentPath[-1] -ne "\")
 +            {
 +                $parentPath += "\";
 +            }
            if (Test-Path $localPath -PathType Leaf)             if (Test-Path $localPath -PathType Leaf)
Line 100: Line 123:
            else             else
            {             {
-                $files = Get-ChildItem $localPath -Recurse -File | Select-Object -ExpandProperty FullName+                $files = 
 + ···················Get-ChildItem $localPath -Recurse -File | 
 + ···················Select-Object -ExpandProperty FullName
            }             }
            foreach ($file in $files)             foreach ($file in $files)
            {             {
-                $entryName = $file.Replace(($parentPath + "\"), "") +                $entryName = $file.Replace($parentPath, "") 
-                Write-Host ("Adding {0}..." -f $entryName) +                Write-Host "Adding $entryName..." 
-                [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $file, $entryName) | Out-Null+                [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile( 
 +····················$zip, $file, $entryName) | Out-Null
            }             }
        }         }
Line 114: Line 140:
    }     }
-    Write-Host ("Archive {0} created, uploading..." -f $archiveName)+    Write-Host "Archive $archiveName created, uploading..."
    # Load WinSCP .NET assembly     # Load WinSCP .NET assembly
Line 133: Line 159:
        $session.Open($sessionOptions)         $session.Open($sessionOptions)
-        $session.PutFiles($session.EscapeFileMask($archivePath), $remotePath).Check()+        $session.PutFileToDirectory($archivePath, $remotePath) | Out-Null
-        Write-Host ("Archive {0} uploaded." -f $archiveName)+        Write-Host "Archive $archiveName uploaded."
-        & "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath"+        # Refresh the remote directory in WinSCP GUI, if -refresh switch was used 
 +        if ($refresh -and $needRefresh) 
 +        { 
 +            Write-Host "Reloading remote directory..." 
 +············& "$env:WINSCP_PATH\WinSCP.exe" "$sessionUrl" /refresh "$remotePath" 
 +        }
    }     }
    finally     finally
Line 148: Line 179:
    $result = 0     $result = 0
} }
-catch [Exception]+catch
{ {
-    Write-Host ("Error: {0}" -f $_.Exception.Message)+    Write-Host "Error: $($_.Exception.Message)"
    $result = 1     $result = 1
} }
Line 164: Line 195:
</code> </code>
-===== Options =====+===== [[options]] Options ===== 
 + 
 +&screenshotpict(extension_zip_and_upload) 
In the //Archive name// enter a name of the archive (without extension) to create. The option is available when executing the extension only. In the //Archive name// enter a name of the archive (without extension) to create. The option is available when executing the extension only.
-When the //Use 7-zip// is not checked, the extension uses a native .NET framework implementation for a ZIP compression. In this mode, the extension has no additional dependency. Particularly, if you want to use a different archive type, check the //Use 7-zip// and install the [[http://www.7-zip.org/download.html|7-Zip]].+When the //Use 7-zip// is not checked, the extension uses a native .NET framework implementation for a ZIP compression. In this mode, the extension has no additional dependency. Particularly, if you want to use a different archive type, check the //Use 7-zip// and install the [[https://www.7-zip.org/download.html|7-Zip]].
When using 7-Zip, you can use the //Archive type// to select the archive type to create. When using 7-Zip, you can use the //Archive type// to select the archive type to create.
Line 173: Line 207:
Use the //7-zip path// to select an alternative path to the ''7z.exe'' or ''7za.exe'', particularly if you are using a portable version. The option is available on the [[ui_pref_commands|Preferences dialog]] only. Use the //7-zip path// to select an alternative path to the ''7z.exe'' or ''7za.exe'', particularly if you are using a portable version. The option is available on the [[ui_pref_commands|Preferences dialog]] only.
-In the //Session log file// you can specify a path to a [[logging|session log file]] (for uploading). The option is available on the [[ui_pref_commands|Preferences dialog]] only.+In the //Session log file//, you can specify a path to a [[logging|session log file]] (for uploading). The option is available on the [[ui_pref_commands|Preferences dialog]] only.
 +In the //Keyboard shortcut//, you can specify a [[custom_key_shortcuts|keyboard shortcut]] for the extension. The option is available on the [[ui_pref_commands|Preferences dialog]] only.

Last modified: by martin