Skip to content

Instantly share code, notes, and snippets.

@kwmt
Last active December 20, 2015 00:18
Show Gist options
  • Save kwmt/6040283 to your computer and use it in GitHub Desktop.
Save kwmt/6040283 to your computer and use it in GitHub Desktop.
How to use "archive/zip"
//pythonchallenge 6
//問題:http://www.pythonchallenge.com/pc/def/channel.html
//ヒント:channel.zipにreadme.txtがある
//使い方:% go run archive_zip.go 90052
package main
import (
"archive/zip"
"fmt"
"io/ioutil"
"os"
"regexp"
)
func main() {
channel(os.Args[1])
}
func channel(nothing string) {
comments := ""
f2c := make(map[string]string)
f2i := make(map[string]int)
r, _ := zip.OpenReader("6_channel.zip") //zip.ReadCloser struct
defer r.Close()
// ファイル名に対するコメントを取得
// ファイル名に対するr.Fileの添字番号を取得
for i, f := range r.File {
f2c[f.Name] = f.Comment
f2i[f.Name] = i
}
for {
nf := nothing + ".txt"
//ファイル名→コメント
comments += f2c[nf]
//ファイル名→それに合った添字のファイルオープン
rc, _ := r.File[f2i[nf]].Open()
content, _ := ioutil.ReadAll(rc)
rc.Close()
re := regexp.MustCompile(`(\d+)$`)
nothing = re.FindString(string(content))
if nothing == "" { //一致しなくなったら終了
fmt.Println(string(content))
break
}
}
fmt.Printf(comments)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment