Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

ไฟล์ ZIP ลดขนาด ไฟล์ รวมหลายไฟล์เป็นไฟล์เดียว ประหยัดพื้นที่ เข้ารหัสข้อมูล แชร์ไฟล์ง่ายขึ้น บทความนี้สอน Zip และ Unzip ด้วย PowerShell ใน Windows 11/10

บีบอัดไฟล์ด้วย PowerShell (Compress-Archive)

ใช้ Cmdlet Compress-Archive ระบุ Path ไฟล์ต้นทาง (คั่นด้วยจุลภาค) และ Destination Path

เปิด PowerShell: Win+X > I

คำสั่งพื้นฐาน:

Compress-Archive -LiteralPath  -DestinationPath 

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

หมายเหตุ: ต้องระบุชื่อไฟล์ .zip ใน Destination ไม่งั้นจะบันทึกเป็น ".zip" เฉยๆ ใช้ Quotes ถ้า Path มี Space

บีบอัดโฟลเดอร์ทั้งหมด (รวม Subfolders) ใช้ Syntax เดียวกัน แต่ระบุ Path โฟลเดอร์แทน

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

ใช้ Wildcard (*) เพื่อความยืดหยุ่น

ต้องใช้ Parameter -Path แทน -LiteralPath (LiteralPath ไม่รับ Wildcard)

  • ไม่รวม Root Folder: เพิ่ม \* ท้าย Path Compress-Archive -Path C:\path\to\file\* -DestinationPath C:\path\to\archive.zip
  • บีบเฉพาะไฟล์ประเภทหนึ่ง (เช่น .docx): Compress-Archive -Path C:\path\to\file\*.docx -DestinationPath C:\path\to\archive.zip (ไม่รวม Subfolders และไฟล์ใน Root)
  • บีบเฉพาะไฟล์ใน Root และ Subdirectories (ไม่รวมโฟลเดอร์): Compress-Archive -Path C:\path\to\file\*.* -DestinationPath C:\path\to\archive.zip

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

อัปเดตไฟล์ ZIP ที่มีอยู่: ใช้ Parameter -Update

Compress-Archive -Path C:\path\to\files -Update -DestinationPath C:\path\to\archive.zip

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

อ่านเพิ่ม: เปิดไฟล์ .TAR.GZ, .TGZ, .GZ

แตกไฟล์ ZIP ด้วย PowerShell (Expand-Archive)

ง่ายกว่าการบีบอัด ต้องการแค่ Source Zip และ Destination

Expand-Archive -LiteralPath  -DestinationPath 

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

ถ้าไม่ระบุ -DestinationPath จะแตกที่ Current Directory สร้างโฟลเดอร์ชื่อ Zip file

ตัวอย่าง: ระบุโฟลเดอร์ Docs จะสร้าง C:\Users\...\Docs และแตกไฟล์เข้าไป

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10

ถ้าโฟลเดอร์ปลายทางมีอยู่แล้ว จะ Error ต้องใช้ -Force เพื่อทับ (ระวัง: จะทับทับไฟล์เก่า 永久)

ครบแล้วครับ วิธี Zip/Unzip ด้วย PowerShell ใน Windows 11/10!

บีบอัดและแตกไฟล์ ZIP ด้วย PowerShell ใน Windows 11/10