如何提取tar.gz,包括多字节字符和windows上禁止的字符
本文关键字:字符 多字节 windows 禁止 包括 何提取 提取 gz tar | 更新日期: 2023-09-27 18:28:23
我尝试使用以下库提取tar.gz,但没有一个能完美工作
我创建tar.gz的环境是linux,提取的环境是windows
我不能触摸linux服务器,因为该服务器是其他公司的。
M: 多字节字符
W: Windows禁止使用的字符(:;/''(¥)|,*?"<>)
SharpZiplib(C#)
M: NG
W: 可以将禁止的字符替换为其他字符
Stream st = new GZipInputStream(File.OpenRead(attFile));
TarArchive archive = TarArchive.CreateInputTarArchive(st, TarBuffer.DefaultBlockFactor);
archive.SetKeepOldFiles(false);
archive.AsciiTranslate = false;
archive.SetUserInfo(0, "", 0, "None");
Directory.CreateDirectory(Work);
archive.ExtractContents(Work);
archive.Close();
tar32.dll(C#)
M: 确定
W: 数据未生成
public static void ExtractTarWithDLL(string Targzpath)
{
IntPtr i = new IntPtr();
string szCmdLine = "-x " + Targzpath + " -o " + Path.GetDirectoryName(Targzpath);
Console.WriteLine(Path.GetDirectoryName(Targzpath));
StringBuilder error = new StringBuilder(512);
Tar(i, szCmdLine, error, 512);
}
tar cs(C#)
M: NG
W: 可以将禁止的字符替换为其他字符
/// <summary>
/// Example of tar-cs library usage to extract tar.gz-archives.
/// Please use the latest version (from trunk) of the library.
/// </summary>
public static class TarGZip
{
public static void Extract(string inputFile, string outputDirectory)
{
using (FileStream inputStream = File.OpenRead(inputFile))
using (Stream tarStream = UnGZipSteam(inputStream))
{
var tarReader = new TarReader(tarStream);
while (tarReader.MoveNext(false)) // Moves pointer to the next file in the tar archive.
{
ExtractTarEntry(tarReader, outputDirectory);
}
}
}
/// <summary>
/// Since GZipStream.Position Property is not implemented,
/// it is necessary to use MemoryStream as intermediate storage.
/// </summary>
/// <param name="inputStream">The input stream.</param>
/// <returns>Un-gzipped stream.</returns>
private static Stream UnGZipSteam(Stream inputStream)
{
using (GZipStream gZipStream = new GZipStream(inputStream, CompressionMode.Decompress))
{
MemoryStream memoryStream = new MemoryStream();
gZipStream.CopyTo(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}
}
private static void ExtractTarEntry(TarReader tarReader, string outputDirectory)
{
string relativePath = tarReader.FileInfo.FileName;
// Relative path can contain slash, not backslash.
// Use Path.GetFullPath() method to convert path.
//relativePath = relativePath.Replace('?','');
string fullPath = Path.GetFullPath(Path.Combine(outputDirectory, relativePath));
//string fullPath = Path.GetFullPath(Path.Combine(outputDirectory, "windows.txt"));
switch (tarReader.FileInfo.EntryType)
{
case EntryType.File:
case EntryType.FileObsolete:
using (FileStream outputStream = File.Create(fullPath))
{
// Read data from a current file to a Stream.
tarReader.Read(outputStream);
}
break;
case EntryType.Directory:
Directory.CreateDirectory(fullPath);
break;
default:
throw new NotSupportedException("Not supported entry type: " + tarReader.FileInfo.EntryType);
}
}
}
tarlib(C++)
M: 数据未生成
W: 数据未生成
我使用的是这个示例代码。http://www.codeproject.com/Articles/470999/tarlib-Windows-TAR-Library
ant.jar(java)
M: 默认情况下,多字节字符被替换为"_"
W: 抛出java.io.FileNotFoundException
public class Targz {
public void extract(Path path) throws IOException {
if(!path.toString().endsWith(".tar.gz")){
throw new Error("extension must be tar.gz.");
}
TarInputStream tin = new TarInputStream(new GZIPInputStream(new FileInputStream(path.toFile())));
for(TarEntry tarEnt = tin.getNextEntry(); tarEnt != null; tarEnt = tin.getNextEntry()) {
if(tarEnt.isDirectory()){
new File(tarEnt.getName()).mkdir();
}
else {
FileOutputStream fos = new FileOutputStream(new File(tarEnt.getName()));
tin.copyEntryContents(fos);
}
}
tin.close();
}
}
有人能帮我吗?谢谢
尝试提取以下tar.gz库,但所有库都不能完美工作。创建tar.gz的环境是linux,提取它的环境是windows。我不能触摸linux服务器,因为该服务器是其他公司的。
嗨,你试过了吗http://www.7-zip.org/?