Bootstrap

用go语言实现文件解压(tar格式)

package main

import (
	"archive/tar"
	"flag"
	"fmt"
	"os"
)

func main() {
	// 从控制台读取文件名
	filename := flag.String("n", "", "文件名")
	flag.Parse()
	file, err := os.Open(*filename)
	if err != nil {
		fmt.Println("文件读取失败")
	}
	data := make([]byte, 300000)
	reader := tar.NewReader(file)
	next, err := reader.Next()
	for next != nil {
		read, _ := reader.Read(data)
		err = os.WriteFile(next.Name, data[:read], 0666)
		next, err = reader.Next()
	}
	fmt.Println("解压成功")

}

⚡ ./tar -n /Users/yang/Desktop/归档.tar
解压成功

;