压缩解压换成Powershell实现
Browse files- v-corpus-zh/pigz.ps1 +60 -25
- v-corpus-zh/pigz_d.ps1 +60 -25
v-corpus-zh/pigz.ps1
CHANGED
@@ -1,25 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
)
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
}
|
23 |
-
|
24 |
-
|
25 |
-
CompressFiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Function Gzip-File([ValidateScript({Test-Path $_})][string]$File){
|
2 |
+
|
3 |
+
$srcFile = Get-Item -Path $File
|
4 |
+
$newFileName = "$($srcFile.FullName).gz"
|
5 |
+
|
6 |
+
try
|
7 |
+
{
|
8 |
+
$srcFileStream = New-Object System.IO.FileStream($srcFile.FullName,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read))
|
9 |
+
$dstFileStream = New-Object System.IO.FileStream($newFileName,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None))
|
10 |
+
$gzip = New-Object System.IO.Compression.GZipStream($dstFileStream,[System.IO.Compression.CompressionMode]::Compress)
|
11 |
+
$srcFileStream.CopyTo($gzip)
|
12 |
+
}
|
13 |
+
catch
|
14 |
+
{
|
15 |
+
Write-Host "$_.Exception.Message" -ForegroundColor Red
|
16 |
+
}
|
17 |
+
finally
|
18 |
+
{
|
19 |
+
$gzip.Dispose()
|
20 |
+
$srcFileStream.Dispose()
|
21 |
+
$dstFileStream.Dispose()
|
22 |
+
}
|
23 |
+
}
|
24 |
+
|
25 |
+
function CompressFiles {
|
26 |
+
param (
|
27 |
+
[string]$Path
|
28 |
+
)
|
29 |
+
|
30 |
+
# ����Ŀ¼
|
31 |
+
Set-Location -Path $Path
|
32 |
+
|
33 |
+
# ѹ����ǰĿ¼�е����� .txt �ļ�
|
34 |
+
$txtFiles = Get-ChildItem -Path . -Filter *.txt
|
35 |
+
# ��������.txt�ļ�
|
36 |
+
foreach ($file in $txtFiles) {
|
37 |
+
# ��ȡ�ļ�������·��
|
38 |
+
$filePath = $file.FullName
|
39 |
+
|
40 |
+
# ѹ���ļ�
|
41 |
+
echo $filePath
|
42 |
+
Gzip-File $filePath
|
43 |
+
}
|
44 |
+
|
45 |
+
# ��ȡ��Ŀ¼
|
46 |
+
$subDirectories = Get-ChildItem -Directory
|
47 |
+
|
48 |
+
# �ݹ���ú�������ÿ����Ŀ¼
|
49 |
+
foreach ($subDir in $subDirectories) {
|
50 |
+
CompressFiles -Path $subDir.FullName
|
51 |
+
}
|
52 |
+
|
53 |
+
# �����ϼ�Ŀ¼
|
54 |
+
Set-Location -Path ..
|
55 |
+
}
|
56 |
+
|
57 |
+
# ���ú�����������ʼ·��
|
58 |
+
$sPath = (Get-Location).Path
|
59 |
+
CompressFiles -Path $sPath
|
60 |
+
Set-Location -Path $sPath
|
v-corpus-zh/pigz_d.ps1
CHANGED
@@ -1,25 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
}
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Function Gunzip-File([ValidateScript({Test-Path $_})][string]$File){
|
2 |
+
|
3 |
+
$srcFile = Get-Item -Path $File
|
4 |
+
$newFileName = Join-Path -Path $srcFile.DirectoryName -ChildPath $srcFile.BaseName
|
5 |
+
|
6 |
+
try
|
7 |
+
{
|
8 |
+
$srcFileStream = New-Object System.IO.FileStream($srcFile.FullName,([IO.FileMode]::Open),([IO.FileAccess]::Read),([IO.FileShare]::Read))
|
9 |
+
$dstFileStream = New-Object System.IO.FileStream($newFileName,([IO.FileMode]::Create),([IO.FileAccess]::Write),([IO.FileShare]::None))
|
10 |
+
$gzip = New-Object System.IO.Compression.GZipStream($srcFileStream,[System.IO.Compression.CompressionMode]::Decompress)
|
11 |
+
$gzip.CopyTo($dstFileStream)
|
12 |
+
}
|
13 |
+
catch
|
14 |
+
{
|
15 |
+
Write-Host "$_.Exception.Message" -ForegroundColor Red
|
16 |
+
}
|
17 |
+
finally
|
18 |
+
{
|
19 |
+
$gzip.Dispose()
|
20 |
+
$srcFileStream.Dispose()
|
21 |
+
$dstFileStream.Dispose()
|
22 |
+
}
|
23 |
+
}
|
24 |
+
|
25 |
+
function DecompressFiles {
|
26 |
+
param (
|
27 |
+
[string]$Path
|
28 |
+
)
|
29 |
+
|
30 |
+
# ����Ŀ¼
|
31 |
+
Set-Location -Path $Path
|
32 |
+
|
33 |
+
# ��ѹ��ǰĿ¼�е����� .txt.gz �ļ�
|
34 |
+
$txtFiles = Get-ChildItem -Path . -Filter *.txt.gz
|
35 |
+
# ��������.txt.gz�ļ�
|
36 |
+
foreach ($file in $txtFiles) {
|
37 |
+
# ��ȡ�ļ�������·��
|
38 |
+
$filePath = $file.FullName
|
39 |
+
|
40 |
+
# ��ѹ�ļ�
|
41 |
+
echo $filePath
|
42 |
+
Gunzip-File $filePath
|
43 |
+
}
|
44 |
+
|
45 |
+
# ��ȡ��Ŀ¼
|
46 |
+
$subDirectories = Get-ChildItem -Directory
|
47 |
+
|
48 |
+
# �ݹ���ú�������ÿ����Ŀ¼
|
49 |
+
foreach ($subDir in $subDirectories) {
|
50 |
+
DecompressFiles -Path $subDir.FullName
|
51 |
+
}
|
52 |
+
|
53 |
+
# �����ϼ�Ŀ¼
|
54 |
+
Set-Location -Path ..
|
55 |
+
}
|
56 |
+
|
57 |
+
# ���ú�����������ʼ·��
|
58 |
+
$sPath = (Get-Location).Path
|
59 |
+
DecompressFiles -Path $sPath
|
60 |
+
Set-Location -Path $sPath
|