title: Reading and uploading zip files from Powershell
body: <p>My below code successfully read a folder and uploades the subfolder one by one as a zip file in SharePoint. But if any of my file is of Zip extension, it gives me an error. I would like to know how can I fix my existing code below to handle the zip files as well so that it loads both the zip folder and non-zip files and folders in the SharePoint. I tried multiple tings like removing .zip extension in the Create-Archive and file but it does not work.
  error received:</p>
  <pre class="lang-none prettyprint-override"><code>2020-12-09T15:49:05.7979920Z Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
  2020-12-09T15:49:05.7981233Z At D:\a\r1\a\_DevOpsScripts\ReleaseNoteScripts\UploadInSharePoint.ps1:55 char:17
  2020-12-09T15:49:05.7982104Z + ... $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT ...
  2020-12-09T15:49:05.7982819Z +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2020-12-09T15:49:05.7983312Z     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
  2020-12-09T15:49:05.7983979Z    eption
  2020-12-09T15:49:05.7984369Z     + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
  </code></pre>
  <pre><code>param($ClientID,$ClientSecret,$WId,$RName,$RReqFor,$SPid,$LibId)
  $tempDirectory = &quot;temp&quot;
  # arguments defined from predefined variables in argument section of powershell stage of pipeline
  $workingDirectory = &quot;$WId&quot;
  $CLIENT_ID = &quot;$ClientID&quot;
  $CLIENT_SECRET = &quot;$ClientSecret&quot;
  $SharePointSiteId = &quot;$SPid&quot;
  $LibraryId = &quot;$LibId&quot;
  $azx = &quot;$RName&quot; +&quot;_&quot;+&quot;$RReqFor&quot;
  $adx = ($azx.Split(&quot;[&quot;)[0])
  $artifactname = $adx.Substring(0,$adx.Length-1)
  
  
  # Get access token
  Function GET-TOKEN 
  {
      param($clientId, $clientSecret)
      $tokenHeaders = New-Object &quot;System.Collections.Generic.Dictionary[[String],[String]]&quot;
      $tokenHeaders.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
      $tokenBody = &quot;client_id=&quot;+ $clientId + &quot;&amp;scope=https%3A//graph.microsoft.com/.default&amp;client_secret=&quot; + $clientSecret + &quot;&amp;grant_type=client_credentials&quot;
      $tokenBody
      $tokenResponse = Invoke-RestMethod 'https://login.microsoftonline.com/test.onmicrosoft.com/oauth2/v2.0/token' -Method 'POST' -Headers $tokenHeaders -Body $tokenBody
      return $tokenResponse    
  }
  
  
  # upload the file
  Function UPLOAD-FILE
  {
      param($workingDir, $tempDir, $clientId, $clientSecret, $artifactname)
    
      
      Get-ChildItem $workingDir | ForEach-Object {
      
      $name = $_.FullName.Split(&quot;\&quot;)[-1]
      CREATE-ARCHIVE -workingDir $_.FullName -tempDir $tempDir -name $name
      $file = $_.FullName + &quot;\&quot; +$tempDir + &quot;\&quot; + &quot;$($name).zip&quot;         
      
      $fileSize = (Get-Item $file).length       
      $uploadURLObject = GET-UPLOADLINK -clientId $clientId -clientSecret $clientSecret -artifactname $name
      $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
      $uploadHeaders = New-Object &quot;System.Collections.Generic.Dictionary[[String],[String]]&quot;
      $uploadHeaders.Add(&quot;Content-Type&quot;, &quot;application/json&quot;)
      $uploadHeaders.Add(&quot;Content-Range&quot;, &quot;bytes &quot; + 0 +&quot;-&quot; + ($fileSize-1) + &quot;/&quot; + $fileSize)
      $uploadHeaders.Add(&quot;Content-Length&quot;, $fileSize)
      $uploadHeaders.Add(&quot;Authorization&quot;, &quot;Bearer &quot;+ $tokenObject.access_token)    
      $uploadBody = [System.IO.File]::ReadAllBytes($file)
      $response = Invoke-RestMethod $uploadURLObject.uploadUrl -Method 'PUT' -Headers $uploadHeaders -Body $uploadBody
      $response | ConvertTo-Json  
      REMOVE-TEMPDIR -workingDir $_.FullName -tempDir $tempDirectory
      }
      
      
  }
  Function GET-UPLOADLINK
  {
      param($clientId, $clientSecret, $artifactname)
      $tokenObject = GET-TOKEN -clientId $clientId -clientSecret $clientSecret
      $uploadLinkRequestHeaders = New-Object &quot;System.Collections.Generic.Dictionary[[String],[String]]&quot;
      $uploadLinkRequestHeaders.Add(&quot;Content-Type&quot;, &quot;application/json&quot;)
      $uploadLinkRequestHeaders.Add(&quot;Authorization&quot;, &quot;Bearer &quot;+ $tokenObject.access_token)
      $uploadLinkRequestBody = &quot;&quot;     
      #update the actual link  
      $fullname = &quot;https://graph.microsoft.com/v1.0/sites/$($SPid)/drives/$($LibId)/root/children/&quot;    
      $comname = $fullname + $artifactname    
      $uploadLinkResponse = Invoke-RestMethod &quot;$($comname).zip/createUploadSession&quot; -Method 'POST' -Headers $uploadLinkRequestHeaders -Body $uploadLinkRequestBody
      $uploadLinkResponse | ConvertTo-Json
      return $uploadLinkResponse    
  }
  
  Function CREATE-ARCHIVE
  {
      param($workingDir, $tempDir, $name )
      write-host $workingDir
      cd $workingDir
      md $tempDir
      #Compress-Archive -Path $workingDir -DestinationPath $workingDir\$tempDir\$artifactname
      Compress-Archive -Path $workingDir -DestinationPath $workingDir\$tempDir\$name
  }
  
  Function REMOVE-TEMPDIR
  {
      param($workingDir, $tempDir)
      #This will be the last step
      rm $workingDir\$tempDir -Recurse   
  }
  
  
  
  UPLOAD-FILE -workingDir $workingDirectory -tempDir $tempDirectory -clientId $CLIENT_ID -clientSecret $CLIENT_SECRET -artifactname $artifactname
  </code></pre>