Zip文件夹内没有根文件夹的内容 [英] Zip content inside folder without the root folder
问题描述
我想将内容中的某个目录压缩成zip文件
例如。假设我有这个目录结构
dir1
file1.html
file2.go
现在我想将它压缩到 dir1.zip
,它是工作
当我提取它时,我得到了相同的结构...
我想压缩内部的内容,当我解压缩它在解压缩之后以root用户的身份得到没有`dir1'文件夹的文件。
$ b $ pre $ file1.html
file2。 go
我试着用这段代码玩这个路径,它不起作用,
我知道我在这里错过了什么?
我已经尝试了
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 (来源)
如果犯错!=无{
返回nil
}
var baseDir字符串
如果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
}
编写器,err:= archive.CreateHeader(header)
if err!= nil {
return err
}
if info.IsDir(){
return nil
}
文件,错误:= os.Open(路径)
if err!= nil {
return err
}
推迟file.Close()
_,err = io.Copy(writer,file)
return err
})
return err
}
dir.Zipit(path / dir1+ /,test+。zip)
或者GO中有更简单的方法如何实现这一点?
假设你正在调用你的函数,如下所示:
Zipit(dir1 /,dir1.zip)
您只需移除要添加到档案文件中的文件名的 baseDir
。
<您目前有以下代码:
if baseDir!={
header.Name = filepath。 Join(baseDir,strings.TrimPrefix(path,source))
}
baseDir
这里是 dir1
。
简单地省略 BASEDIR
(但要保留修剪前缀):
header.Name = strings.TrimPrefix(path,source)
这与解压缩具有不同名称的工件,您只需修改 header.Name
即可。这听起来像你需要检查各种 filepath
函数,看看它们可以如何帮助你。
I want to zip some directory inside content into zip file
e.g. assume i’ve this directory structure
dir1
file1.html
file2.go
Now I want to zip it to dir1.zip
which is working
when I extract it I got the same structure... I want to zip the content inside that when I unzip it I get the files inside without the `dir1' folder as root after extracting it
file1.html
file2.go
I try to play with the path’s with this code and it doesn’t work, Any idea what I miss here ?
i've tryied
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
}
dir.Zipit("path/dir1" +"/", "test"+".zip")
Or maybe there is simpler way in GO to achieve this?
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.
这篇关于Zip文件夹内没有根文件夹的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!