duanliusong6395
2018-02-18 21:05
浏览 5
已采纳

创建不带标题的zip文件

I’ve two zip’s ,and I use a zip command to check the difference between the two zip, diff -y (unzip -l old.zip | psub) (unzip -l new.zip | psub) I need them to be exactly the same

First created in old tool which look like this and it’s OK

 Length      Date    Time    Name                
---------  ---------- -----   ----              
      158         02-18-2018 12:26   META/MANI.MF        
      153         02-18-2018 12:26   META/mt.yaml        
  3956032         02-18-2018 12:26   ui/data.zip                    
---------                     -------       ---------                    
  3956343                     3 files                       

This is the zip with the new tool which look different

  Length      Date    Time    Name              
---------  ---------- -----   ----               
|           0  02-18-2018 20:37           ./
|           0  02-18-2018 20:37           META/
|         150  02-18-2018 20:37           META/MANI.MF
>         178  02-18-2018 20:37           META/mt.yaml
>           0  02-18-2018 20:37           ui/
>     3980703  02-18-2018 20:37           ui/data.zip

if we look at the ui folder (zip of the new tool) you see that in the new there is two entries

  ui/
  ui/data.zip

And I need only the second as in the old tool, the file structure should be identical

The logic to build it is like this

  1. during the program process I’ve folder which is called `ui’
  2. I zip it with the function Zipit(see code below) on the level so I’ve it like this root - ui - ui.zip
  3. then I remove the ui becouse I need just the zip and it contain a lot of files that not needed after the zip process
  4. create new empty folder ui os.MkdirAll(path, os.ModePerm)
  5. Mmove the zip inside the new ui folder - os.Rename(path+".zip", path+"/"+"data.zip")

What am I doing wrong here ? I need the new structue will be inside the zip exactly the same

func Zipit(params ...string) error {


    zipfile, err := os.Create(params[1])
    if err != nil {
        return err
    }
    defer zipfile.Close()

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

    info, err := os.Stat(params[0])
    if err != nil {
        return nil
    }

    var baseDir string
    if info.IsDir(); len(params) > 2 {
        baseDir = params[2]
    } else {
        baseDir = filepath.Base(params[0])

    }

    filepath.Walk(params[0], 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(strings.TrimPrefix(path, params[0]))
        }

        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
}


to see the difference between the zip 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchengchu8374 2018-02-18 21:24
    已采纳

    It looks like you need to skip adding directories to the zip file header, every time you see a directory in the filepath.Walk callback just skip it:

    filepath.Walk(params[0], func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }
    
        // I moved this check to the beginning of the callback.
        if info.IsDir() {
            return nil
        }
    
        header, err := zip.FileInfoHeader(info)
        if err != nil {
            return err
        }
    
        if baseDir != "" {
            header.Name = filepath.Join(strings.TrimPrefix(path, params[0]))
        }
    
        header.Method = zip.Deflate
    
        writer, err := archive.CreateHeader(header)
        if err != nil {
            return err
        }
    
        file, err := os.Open(path)
        if err != nil {
            return err
        }
        defer file.Close()
        _, err = io.Copy(writer, file)
        return err
    })
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题