dqd78456 2019-05-28 08:31
浏览 39

如何修复Windows上压缩的os.PathSeparator解压缩文件?

I'm zipping files on Windows and when unzip on Linux, using the os default application, it doesn't create the folders along the path but a single file named "indexes\search.invcus\index_meta.json" e.g.:

unzip 2019-05-23_113735_data.zip -d xfolder
Archive:  2019-05-23_113735_data.zip
  inflating: xfolder/indexes\search.invcus\index_meta.json  
  inflating: xfolder/indexes\search.invcus\store  
  inflating: xfolder/indexes\search.items\index_meta.json  
  inflating: xfolder/indexes\search.items\store  
  inflating: xfolder/indexes\search.subjects\index_meta.json  
  inflating: xfolder/indexes\search.subjects\store  
  inflating: xfolder/indexes\search.users\index_meta.json  
  inflating: xfolder/indexes\search.users\store  
  inflating: xfolder/data.db         

I get the correct folders and files only if the zip file is created on Linux and unzipped on Windows using 7zip, etc...

func zipFiles(filename string, files []string) error {

    newZipFile, err := os.Create(filename)
    if err != nil {
        return err
    }
    defer newZipFile.Close()

    zipWriter := zip.NewWriter(newZipFile)
    defer zipWriter.Close()

    // Add files to zip
    for _, file := range files {
        if err = addFileToZip(zipWriter, file); err != nil {
            return err
        }
    }
    return nil
}


func addFileToZip(zipWriter *zip.Writer, filename string) error {
    fileToZip, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer fileToZip.Close()

    // Get the file information
    info, err := fileToZip.Stat()
    if err != nil {
        return err
    }

    header, err := zip.FileInfoHeader(info)
    if err != nil {
        return err
    }

    // Using FileInfoHeader() above only uses the basename of the file. If we want
    // to preserve the folder structure we can overwrite this with the full path.
    header.Name = filename

    // Change to deflate to gain better compression
    // see http://golang.org/pkg/archive/zip/#pkg-constants
    header.Method = zip.Deflate

    writer, err := zipWriter.CreateHeader(header)
    if err != nil {
        return err
    }
    _, err = io.Copy(writer, fileToZip)
    return err
}

I expect:

 indexes (folder)
     search.invcus (folder)
         index_meta.json (file)

  • 写回答

1条回答 默认 最新

  • dsiftnc99059 2019-05-28 08:47
    关注

    Did you check out convert a windows created zip to Linux internal paths issue? Seems like something gone wrong while creating that zip file.

    Or alternatively, from those answers:

    Use 7z rn to rename the files within the archive so that they have a forward slash. Then when you extract the archive, directories will be created.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?