Brother Lian Blockchain training share go language compression and decompression

Source: Internet
Author: User

In the first quarter of 2018, the demand for blockchain-related talent had reached 9.7 times times the same period in 2017, with a 4.6 times-fold increase in the number of companies publishing blockchain-related posts.


Brother Lian Education go all-in-one and Blockchain training courses are unique, structured courses developed by Tsinghua, Microsoft, and Google teachers over the course of a half-year period.

Compress files

Package Main

Import (

"FMT"

"OS"

"IO"

"Archive/tar"

"Compress/gzip"

)


Func Main () {

File Write

FW, ERR: = OS. Create ("tar/lin_golang_src.tar.gz")

If err! = Nil {

Panic (ERR)

}

Defer FW. Close ()


Gzip Write

GW: = gzip. Newwriter (FW)

Defer GW. Close ()


Tar Write

TW: = tar. Newwriter (GW)

Defer TW. Close ()


Open Folder

Dir, err: = OS. Open ("file/")

If err! = Nil {

Panic (Nil)

}

Defer dir. Close ()


Read a list of files

FIS, err: = dir. Readdir (0)

If err! = Nil {

Panic (ERR)

}


Traverse file List

For _, Fi: = Range fis {

Escape the folder, I will not be recursive here

If Fi. Isdir () {

Continue

}


Print file name

Fmt. Println (FI. Name ())


Open File

FR, err: = OS. Open (dir. Name () + "/" + FI. Name ())

If err! = Nil {

Panic (ERR)

}

Defer Fr. Close ()


Information header

H: = new (tar. Header)

H.name = fi. Name ()

H.size = fi. Size ()

H.mode = Int64 (FI. Mode ())

H.modtime = fi. Modtime ()


Write message header

Err = tw. Writeheader (h)

If err! = Nil {

Panic (ERR)

}


Write a file

_, err = Io. Copy (TW, FR)

If err! = Nil {

Panic (ERR)

}

}


Fmt. Println ("tar.gz OK")

}

Unzip the file


Package Main


Import (

"FMT"

"OS"

"IO"

"Time"

"Archive/tar"

"Compress/gzip"

)


Func Main () {

File read

FR, err: = OS. Open ("tar/lin_golang_src.tar.gz")

If err! = Nil {

Panic (ERR)

}

Defer Fr. Close ()


Gzip Read

GR, err: = gzip. Newreader (FR)

If err! = Nil {

Panic (ERR)

}

Defer Gr. Close ()


Tar read

TR: = tar. Newreader (GR)


Read file

for {

H, err: = tr. Next ()

If err = = Io. EOF {

Break

}

If err! = Nil {

Panic (ERR)

}


Show Files

Fmt. Println (H.name)


Open File

FW, ERR: = OS. OpenFile ("file2/" + h.name, os. O_create | Os. O_wronly, 0644/*os. FileMode (H.mode) */)

If err! = Nil {

Panic (ERR)

}

Defer FW. Close ()


Write a file

_, err = Io. Copy (FW, TR)

If err! = Nil {

Panic (ERR)

}


}


Fmt. Println ("Un tar.gz OK")

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.