解压targz(什么命令解压缩targz文件)

发表时间:2023-12-03 14:24:01
0 0

解压tar,gz,什么命令解压缩targz文件。小编来告诉你更多相关信息。

影像文件组织

百度了一下,以·tar.gz为后缀的文件是一种压缩文件,在Linux和macOS下常见,Linux和macOS都可以直接解压使用这种压缩文件。在windows下的WinRAR也可以使用,相当于常见的RAR和ZIP格式。

而面对大量文件中的部分数据读取,自然想到了程序化解决。

此处用到ICSharpCode.SharpZipLib库,首先需要添加ICSharpCode.SharpZipLib.dll的引用,本文引用的下载地址为:https://pan.baidu.com/s/1-MK-n3RWg5JUFirlC8cwcw

程序思路主要是2步,第一步是将tar.gz解压为tar格式文件,第二部再从tar文件中将指定文件导出,最后删除tar文件完成操作。

解压tar.gz文件

publicboolgetTarGzFile(stringTargetFile,stringsaveto,stringfileformat)

stringrootFile="";

stringfilepath=TargetFile;

//读取压缩文件(zip文件),准备解压缩

GZipInputStreamgzs=newGZipInputStream(File.OpenRead(filepath.Trim()));

stringtarfilename=Path.Combine(Path.GetDirectoryName(TargetFile),Path.GetFileNameWithoutExtension(TargetFile));

FileStreamdestFile=File.Open(tarfilename,FileMode.Create);

try

intbuffersize=2048;//缓冲区的尺寸,一般是2048的倍数

byte[]FileData=newbyte[buffersize];//创建缓冲数据

while(buffersize>0)//一直读取到文件末尾

buffersize=gzs.Read(FileData,0,buffersize);//读取压缩文件数据

destFile.Write(FileData,0,buffersize);//写入目标文件

catch(Exceptionee)

Console.WriteLine(ee.Message);

destFile.Close();//关闭目标文件

gzs.Close();//关闭压缩文件

boolresult=UnZip(tarfilename,saveto,fileformat);

returnresult;

从tar文件中提取指定文件

在上一步倒数第三行有一个UnZip函数就是提取指定格式文件,提取完成后删除tar文件

privateboolUnZip(stringfileToUnZip,stringzipedFolder,stringfileformat)

boolresult=true;

FileStreamfs=null;

TarEntryent=null;

stringfileName;

if(!File.Exists(fileToUnZip))

returnfalse;

if(!Directory.Exists(zipedFolder))

Directory.CreateDirectory(zipedFolder);

try

TarInputStreamzipStream=newTarInputStream(File.OpenRead(fileToUnZip.Trim()));

while((ent=zipStream.GetNextEntry())!=null)

if(!string.IsNullOrEmpty(ent.Name))

fileName=Path.Combine(zipedFolder,ent.Name);

fileName=fileName.Replace('/','\\');//changebyMr.HopeGi

intindex=ent.Name.LastIndexOf('/');

if(index!=-1||fileName.EndsWith("\\"))

stringtmpDir=(index!=-1?fileName.Substring(0,fileName.LastIndexOf('\\')):fileName)+"\\";

if(!Directory.Exists(tmpDir))

Directory.CreateDirectory(tmpDir);

if(tmpDir==fileName)

continue;

Listformatlb=fileformat.Split(';').ToList();

for(intuu=0;uu<formatlb.Count;uu++)

if(fileName.Contains(formatlb[uu]))

if(File.Exists(fileName))

//获取时间

DateTimedt=DateTime.Now;

stringtimeNow=dt.ToString("yyyy-MM-dd_HH-mm-ss-ff");

fileName=System.IO.Path.GetDirectoryName(fileName)+"\\"+System.IO.Path.GetFileNameWithoutExtension(fileName)+"_"+timeNow+System.IO.Path.GetExtension(fileName);

fs=File.Create(fileName);

intsize=102410244;

byte[]data=newbyte[size];

while(true)

size=zipStream.Read(data,0,data.Length);

if(size>0)

fs.Write(data,0,size);

else

break;

break;

zipStream.Close();

catch

result=false;

finally

if(fs!=null)

fs.Close();

fs.Dispose();

if(ent!=null)

ent=null;

GC.Collect();

GC.Collect(1);

try

File.Delete(fileToUnZip);

catch(Exceptionex)

returnresult;

实现效果

程序界面

最终实现tar.gz压缩文件中指定文件的自动化提取...

声明:本文图片、文字、视频等内容来源于互联网,本站无法甄别其准确性,建议谨慎参考,本站不对您因参考本文所带来的任何后果负责!本站尊重并保护知识产权,本文版权归原作者所有,根据《信息网络传播权保护条例》,如果我们转载内容侵犯了您的权利,请及时与我们联系,我们会做删除处理,谢谢。

全部评论 0条
请先登录发表后评论(·ω·)
表情
发表
1页,跳至

金币

主题
最新发表
返回顶部