使用SharpZipLib制作没有文件夹结构的.tar.gz存档 [英] Make a .tar.gz archive without folder structure using SharpZipLib
问题描述
我正在尝试使用SharpZipLib从一个文件夹中.tar.gz文件列表.问题是无论文件路径如何传递-结果始终包含文件路径-而不仅仅是文件本身.我在这里想念什么?
I am trying to .tar.gz a list of files from a folder using SharpZipLib. The problem is no matter how pass the files paths - the result always contains the files paths - and not only the files thems selves. What am i missing here?
string filesFolder = "c:\\testfolder\\test\\";
List<string> filesToZip = new List<string>() { filesFolder +"test1", filesFolder +"test2"};
using (FileStream fs = new FileStream(filesFolder +"myGz.tar.gz" , FileMode.Create, FileAccess.Write, FileShare.None))
using (Stream gzipStream = new GZipOutputStream(fs))
using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(gzipStream))
{
foreach (string filename in filesToZip )
{
{
TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
tarArchive.WriteEntry(tarEntry, false);
}
}
}
我得到的是一个"myGz.tar.gz"文件.当我尝试使用7.zip打开它时-我在存档中找到了完整的文件夹结构-c:\ testfolder \ test \,并在其中找到了"test1","test".
what i get is a "myGz.tar.gz" files. When i try to open it with 7.zip - i get the full folders structure in the archive - c:\testfolder\test\, and in it - "test1", "test".
如何删除文件路径?
谢谢
推荐答案
我遇到了同样的问题,发现这个问题后就发现了这个问题.
I had the same problem, and I figured it out just after finding this question.
关键是设置tarEntry
的Name
属性,然后再将其添加到存档中.
The key is to set the Name
property of the tarEntry
, before adding it into the archive.
TarEntry tarEntry = TarEntry.CreateEntryFromFile(filename);
tarEntry.Name = Path.GetFileName(filename);
tarArchive.WriteEntry(tarEntry, false);
这篇关于使用SharpZipLib制作没有文件夹结构的.tar.gz存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!