最近做项目,收集到较多原始遥感影像文件,其格式全部为tar.gz,由于文件较大(每个文件均大于1GB),根本没有硬盘来全部解压,且项目只需要用到其中的部分文件,就尝试用C#来读取其中的部分文件。

csharp生成的文件(解压tar.gz压缩文件)(1)

影像文件组织

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

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

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

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

解压tar.gz文件

public bool getTarGzFile(string TargetFile, string saveto, string fileformat)

{

string rootFile = " ";

string filepath = TargetFile;

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

GZipInputStream gzs = new GZipInputStream(File.OpenRead(filepath.Trim()));

string tarfilename = Path.Combine(Path.GetDirectoryName(TargetFile), Path.GetFileNameWithoutExtension(TargetFile));

FileStream destFile = File.Open(tarfilename, FileMode.Create);

try

{

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

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

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

{

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

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

}

}

catch (Exception ee)

{

Console.WriteLine(ee.Message);

}

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

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

bool result = UnZip(tarfilename, saveto, fileformat);

return result;

}

从tar文件中提取指定文件

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

private bool UnZip(string fileToUnZip, string zipedFolder, string fileformat)

{

bool result = true;

FileStream fs = null;

TarEntry ent = null;

string fileName;

if (!File.Exists(fileToUnZip))

return false;

if (!Directory.Exists(zipedFolder))

Directory.CreateDirectory(zipedFolder);

try

{

TarInputStream zipStream = new TarInputStream(File.OpenRead(fileToUnZip.Trim()));

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

{

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

{

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

fileName = fileName.Replace('/', '\\');//change by Mr.HopeGi

int index = ent.Name.LastIndexOf('/');

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

{

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

if (!Directory.Exists(tmpDir))

{

Directory.CreateDirectory(tmpDir);

}

if (tmpDir == fileName)

{

continue;

}

}

List<String> formatlb = fileformat.Split(';').ToList();

for (int uu = 0; uu < formatlb.Count; uu )

{

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

{

if (File.Exists(fileName))

{

//获取时间

DateTime dt = DateTime.Now;

string timeNow = 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);

int size = 1024 * 1024 * 4;

byte[] data = new byte[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 (Exception ex)

{

}

return result;

}

实现效果

csharp生成的文件(解压tar.gz压缩文件)(2)

程序界面

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

,