【发布时间】:2018-05-09 03:05:39
【问题描述】:
我正在将 c# 应用程序转换为 php。在那个应用程序中使用了 GZipStream,但我不知道如何在 php 中压缩和解压缩相同的东西。
我可以使用 php 进行压缩和压缩,但问题是,php 的 zip 文件和 c# 的 zip 文件都不同。我想要与 c# 完全相同的输出。
我的 C# 和 php 代码都在工作,但两者的输出不同。应该是一样的。
C# 代码:
public static bool CompressDirectory(string sInDir, string sOutFile)
{
try
{
string[] sFiles = Directory.GetFiles(sInDir, "*.*", SearchOption.AllDirectories);
int iDirLen = sInDir[sInDir.Length - 1] == Path.DirectorySeparatorChar ? sInDir.Length : sInDir.Length + 1;
using (FileStream outFile = new FileStream(sOutFile, FileMode.Create, FileAccess.Write, FileShare.None))
using (GZipStream str = new GZipStream(outFile, CompressionMode.Compress))
foreach (string sFilePath in sFiles)
{
string sRelativePath = sFilePath.Substring(iDirLen);
//if (progress != null)
// progress(sRelativePath);
CompressFile(sInDir, sRelativePath, str);
}
}
catch (System.Exception ex)
{
PharmaRackMargSynchronizerLog.WriteEntry("CompressDirectory: " + ex.Message, EventLogEntryType.Error);
return false;
}
return true;
}
static void CompressFile(string sDir, string sRelativePath, GZipStream zipStream)
{
try
{
//Compress file name
char[] chars = sRelativePath.ToCharArray();
zipStream.Write(BitConverter.GetBytes(chars.Length), 0, sizeof(int));
foreach (char c in chars)
zipStream.Write(BitConverter.GetBytes(c), 0, sizeof(char));
//Compress file content
byte[] bytes = File.ReadAllBytes(Path.Combine(sDir, sRelativePath));
zipStream.Write(BitConverter.GetBytes(bytes.Length), 0, sizeof(int));
zipStream.Write(bytes, 0, bytes.Length);
}
catch (System.Exception ex)
{
PharmaRackMargSynchronizerLog.WriteEntry("CompressFile: " + ex.Message, EventLogEntryType.Error);
}
}
我在 PHP 中尝试过同样的事情,为此我使用了 gzcompress 函数,然后在压缩文件内容后我使用 ZipArchive 压缩文件。我可以在这里创建编码和压缩,但是 php 的 zip 的输出和 C# 的 zip 的输出是不同的。我希望它是一样的。
PHP 代码:
// create zip file
$cfilename = REPORTPATH . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "\\" . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "_Firms.csv";
$zipname = date('m-d-Y_H_i_s') . '.zip';
$zip = new ZipArchive();
$zip->open($_SERVER['DOCUMENT_ROOT'] . "/api.pharmarack.com/distributors/" . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "/" . $zipname, ZipArchive::CREATE);
// gzcompress compressiong process
$compressedstring = gzcompress(base64_encode(file_get_contents($cfilename)) , 9);
// $uncompressed = gzuncompress($compressedstring);
// echo $uncompressed;die;
$zip->addFromString($cfilename, $compressedstring);
// $zip->addFile($cfilename);
$zip->close();
// End of Create Zip File
【问题讨论】:
-
那是什么问题?
-
您是否在 google 中输入了“php 中的 zip 目录”? stackoverflow.com/questions/4914750/… :/
-
我能够创建没有问题但没有编码的 zip 文件,并且 zip 与 c# 相同
标签: php compression archive gzipstream