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
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)