英文:
Read CSV file on protected zip
问题
我有一个链接,类似这样:https://storage.googleapis.com/data/test_file.csv.zip
内容是一个受密码保护的zip文件,里面包含一个csv文件。我该如何从csv文件中读取数据?
我尝试了以下代码,但出现错误:
func ReadCSVZIPFromURL(fileURL string) (data [][]string, err error) {
resp, err := http.Get(fileURL)
if err != nil {
return data, errors.AddTrace(err)
}
defer resp.Body.Close()
r, err := zip.OpenReader(resp.Body)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, file := range r.File {
if file.IsEncrypted() {
file.SetPassword("password")
}
reader := csv.NewReader(file)
data, err := reader.ReadAll()
}
return data, nil
}
英文:
I have link like this: https://storage.googleapis.com/data/test_file.csv.zip
the content is one csv file on the protected zip with password. How can I read data from the csv?
I have try this but its error
func ReadCSVZIPFromURL(fileURL string) (data [][]string, err error) {
resp, err := http.Get(fileURL)
if err != nil {
return data, errors.AddTrace(err)
}
defer resp.Body.Close()
r, err := zip.OpenReader(resp.Body)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, file := range r.File {
if file.IsEncrypted() {
file.SetPassword("password")
}
reader := csv.NewReader(file)
data, err := reader.ReadAll()
}
return data, nil
}
</details>
# 答案1
**得分**: 0
我已经解决了这个问题,以下是解决方法。思路是将字节复制到zip读取器中,然后获取ioReader,然后使用csv库读取ioReader。
要使用密码对数据进行加密和解密,我们使用了这个库 "github.com/alexmullins/zip"
```go
func GetCSVFromZipURL(ctx context.Context, fileURL, filePassword string) (ioReader io.Reader, err error) {
span, ctx := tracer.StartSpanFromContext(ctx)
defer span.Finish()
resp, err := http.Get(fileURL)
if err != nil {
return ioReader, errors.AddTrace(err)
}
defer resp.Body.Close()
buf := &bytes.Buffer{}
_, err = io.Copy(buf, resp.Body)
if err != nil {
return ioReader, errors.AddTrace(err)
}
b := bytes.NewReader(buf.Bytes())
r, err := zip.NewReader(b, int64(b.Len()))
if err != nil {
return ioReader, errors.AddTrace(err)
}
for _, f := range r.File {
if f.IsEncrypted() {
f.SetPassword(filePassword)
}
ioReader, err = f.Open()
if err != nil {
return ioReader, errors.AddTrace(err)
}
return ioReader, nil
}
return ioReader, nil
}
func getUserBenefitListFromCSV(ioReader io.Reader) (userBenefitList []UserBenefit, err error) {
reader := csv.NewReader(ioReader)
row := 1
for {
csvRowsStr, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
// check if 1st row (header), skip
if row == 1 {
row++
continue
}
if len(csvRowsStr) > 0 {
userID, err := strconv.ParseInt(csvRowsStr[0], 10, 64)
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
catalogID := 0
if len(csvRowsStr) > 1 {
catalogID, err = strconv.ParseInt(csvRowsStr[1], 10, 64)
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
}
userBenefitTemp := UserBenefit{
UserID: userID,
CatalogID: catalogID,
}
userBenefitList = append(userBenefitList, userBenefitTemp)
}
}
return userBenefitList, nil
}
英文:
i have solve the problem, this the solve. the idea is we copy the byte to zip reader, and we will get the ioReader, the we we read ioReader use csv library.
to encrypt and decrypt data using password we use this library "github.com/alexmullins/zip"
func GetCSVFromZipURL(ctx context.Context, fileURL, filePassword string) (ioReader io.Reader, err error) {
span, ctx := tracer.StartSpanFromContext(ctx)
defer span.Finish()
resp, err := http.Get(fileURL)
if err != nil {
return ioReader, errors.AddTrace(err)
}
defer resp.Body.Close()
buf := &bytes.Buffer{}
_, err = io.Copy(buf, resp.Body)
if err != nil {
return ioReader, errors.AddTrace(err)
}
b := bytes.NewReader(buf.Bytes())
r, err := zip.NewReader(b, int64(b.Len()))
if err != nil {
return ioReader, errors.AddTrace(err)
}
for _, f := range r.File {
if f.IsEncrypted() {
f.SetPassword(filePassword)
}
ioReader, err = f.Open()
if err != nil {
return ioReader, errors.AddTrace(err)
}
return ioReader, nil
}
return ioReader, nil
}
func getUserBenefitListFromCSV(ioReader io.Reader) (userBenefitList []UserBenefit, err error) {
reader := csv.NewReader(ioReader)
row := 1
for {
csvRowsStr, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
// check if 1st row (header), skip
if row == 1 {
row++
continue
}
if len(csvRowsStr) > 0 {
userID, err := strconv.ParseInt(csvRowsStr[0], 10, 64)
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
catalogID := 0
if len(csvRowsStr) > 1 {
catalogID, err = strconv.ParseInt(csvRowsStr[1], 10, 64)
if err != nil {
return userBenefitList, errors.AddTrace(err)
}
}
userBenefitTemp := UserBenefit{
UserID: userID,
CatalogID: catalogID,
}
userBenefitList = append(userBenefitList, userBenefitTemp)
}
}
return userBenefitList, nil
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论