Zip content inside folder without the root folder

前端 未结 1 1684
独厮守ぢ
独厮守ぢ 2020-12-20 09:11

I want to zip some directory inside content into zip file

e.g. assume i’ve this directory structure

dir1
  file1.html
  file2.go

No

相关标签:
1条回答
  • 2020-12-20 09:37

    Assuming you are calling your function as follows:

    Zipit("dir1/", "dir1.zip") 
    

    All you need to do is remove the baseDir that is being added to the filename inside the archive.

    You currently have the following code:

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

    baseDir here is dir1.

    Simply omit the baseDir (but to keep trimming the prefix):

    header.Name = strings.TrimPrefix(path, source)
    

    This is very similar to Unzip artifacts with different name where all you need to do is modify header.Name as you see fit. It sounds like you need to examine the various filepath functions to see how they can help you.

    0 讨论(0)
提交回复
热议问题