dscuu86620 2018-02-11 14:13
浏览 36
已采纳

解压缩具有不同名称的工件

I have a program in which I need to copy the folder contents (from folder1) to a new folder (folder2) in the same dir level (path). Since I cannot give the same name I use tmpFolder name (for folder2). When I finish to do all the logic I need zip the copied folder and I give the zip the name folder1.zip The problem is that when I extract the folder1.zip I see folder2.

I want it to be folder1 after the zip.

Is there some trick I can use to do it?

In addition, I know that I can copy to folder2 in different level (path) but I want to avoid it if possible since the copy can be very expensive when working on big folder contents.

I use this code to zip the folder:

func Zipit(source, target string) error {
    zipfile, err := os.Create(target)
    if err != nil {
        return err
    }
    defer zipfile.Close()

    archive := zip.NewWriter(zipfile)
    defer archive.Close()

    info, err := os.Stat(source)
    if err != nil {
        return nil
    }

    var baseDir string
    if info.IsDir() {
        baseDir = filepath.Base(source)
    }

    filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }

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

        if baseDir != "" {
            header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
        }

        if info.IsDir() {
            header.Name += "/"
        } else {
            header.Method = zip.Deflate
        }

        writer, err := archive.CreateHeader(header)
        if err != nil {
            return err
        }

        if info.IsDir() {
            return nil
        }

        file, err := os.Open(path)
        if err != nil {
            return err
        }
        defer file.Close()
        _, err = io.Copy(writer, file)
        return err
    })

    return err
}
  • 写回答

1条回答 默认 最新

  • doukougua7873 2018-02-11 14:49
    关注

    Names of files and directories in zip archives come from the zip.FileHeader.

    Your code already initializes the header from the os.FileInfo. This is important as it populates metadata such as timestamps and uncompressed size.

    Furthermore, your code seems to be doing the following:

    If invoked with:

    Zipit("/path/to/folder2/", "/path/to/folder1.zip")
    

    Before traversing the directory tree, it computes the base directory:

    baseDir = filepath.Base(source)
    // baseDir is "folder2"
    

    Then for each file, the path inside the archive is set to:

    header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
    

    This turns a filename such as /path/to/folder2/otherdir/myfile into folder2/otherdir/myfile.

    If you want to change the directory name stored inside the archive, you just need to change baseDir to the desired name.

    I would recommend the following solution:

    Change the function signature to:

    func Zipit(source, target, newBaseName string) error {
    

    Change the basedir to:

    if newBaseName != "" {
        baseDir = newBaseName
    else if info.IsDir() {
        baseDir = filepath.Base(source)
    }
    

    Then call your function with:

    Zipit("/path/to/folder2/", "/path/to/folder1.zip", "folder1")
    

    This should result in an archive that extracts into folder1/.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么