gzip-content.ps1 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. # Stop on all errors
  20. $ErrorActionPreference = 'Stop';
  21. Function Gzip-File{
  22. Param(
  23. $inFile,
  24. $outFile = ($inFile + ".gz"),
  25. $force = $false
  26. )
  27. if(-not (Test-Path $inFile)) {
  28. Write-Host "$inFile does not exist"
  29. return $false
  30. }
  31. if((Test-Path $outFile)) {
  32. if(-not $force) {
  33. Write-Host "$outFile already exists"
  34. return $true
  35. } else {
  36. Remove-Item $outFile
  37. }
  38. }
  39. $inputStream = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read)
  40. $outputStream = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None)
  41. $gzipStream = New-Object System.IO.Compression.GzipStream $outputStream, ([IO.Compression.CompressionMode]::Compress)
  42. $buffer = New-Object byte[](1024)
  43. while($true){
  44. $read = $inputStream.Read($buffer, 0, 1024)
  45. if ($read -le 0){break}
  46. $gzipStream.Write($buffer, 0, $read)
  47. }
  48. $gzipStream.Close()
  49. $outputStream.Close()
  50. $inputStream.Close()
  51. Remove-Item $inFile
  52. return $true
  53. }
  54. $errorFound = $false
  55. $files = @()
  56. $force = $false
  57. ForEach ($arg in $args) {
  58. if($arg -eq "-f" -or $arg -eq "--force") {
  59. $force = $true
  60. continue
  61. }
  62. $files += $arg
  63. }
  64. ForEach ($file in $files) {
  65. $input = $file
  66. $output = $file + ".gz";
  67. Write-Host "Running: Gzip-File $input $output $force"
  68. $success = Gzip-File $input $output $force
  69. if(-not $success) {
  70. $errorFound = $true
  71. }
  72. }
  73. if ($errorFound) {
  74. throw "Failed to gzip all files!"
  75. }