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

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀