我曾尝试使用Zlib解压缩文件,但它只是说"数据错误"并给了我一个空档。
这是我尝试过的代码:
// Open a new temp file to write new file to
$tempFile = fopen("tempFile", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);
// Write new decompressed file
fwrite($tempFile, zlib_decode(file_get_contents($path))); // $path = absolute path to data.tar.Z
// close temp file
fclose($tempFile);
我还尝试将其部分解压缩,从.tar.Z到.tar只是一个文件。我尝试使用lzw函数来取消.Z,但我无法使它工作。有没有办法做到这一点?
修改
这是我尝试过的一些代码。只是为了确保file_get_contents
正常运行。我仍然收到"数据错误"。
$tempFile = fopen("tempFile.tar", "w");
// Make sure tempFile is empty
ftruncate($tempFile, 0);
// Write new decompressed file
$contents = file_get_contents($path);
if ($contents) {
fwrite($tempFile, gzuncompress($contents));
}
// close temp file
fclose($tempFile);
EDIT2:我认为LZW无法正常工作的原因是因为.tar.Z文件的内容如下所示:
��3dЀ��0p���a�
H�H��ŋ3j��@�6l�
我试过的LZW函数都在字典中使用ASCII字符。这些是什么角色?
答案 0 :(得分:4)
所以你想用PHP解压缩taz文件原生?试试我的new extension!
<?php
// check on your DB connection
$connDB = new mysqli ( $db_host, $db_username, $db_pass, $db_name );
$date = array("2000-10-01","2000-12-16");
$author = array("Gambardella","Ralls");
$count = 0;
foreach ($dates as $date)
{
$sql_insert = "INSERT into book(something, author, date) values(',','$date','$author[$count]')";
mysqli_query ( $connDB, $sql_insert );
$count ++ ;
}
mysqli_close($connDB);
?>
另请注意,zlib功能不起作用的原因是因为您需要LZW压缩,而不是gzip压缩。
答案 1 :(得分:1)
根据此网址https://kb.iu.edu/d/acsy,您可以尝试
<?php
$file = '/tmp/archive.z';
shell_exec("uncompress $file");
如果您没有类似操作系统的Unix检查https://kb.iu.edu/d/abck以获取适当的程序。
答案 2 :(得分:0)
该文件使用LZW压缩进行压缩,我尝试了一些,但似乎没有可靠的方法在PHP中解压缩这些。 Cosmin的答案包含正确的第一步,但在使用系统的uncompress
实用程序解压缩文件后,仍然需要提取TAR文件。这可以通过PHP的内置工具来完成,以处理其自定义的PHAR文件。
// the file we're getting
$url = "ftp://ftp.ncdc.noaa.gov/pub/data/hourly_precip-3240/05/3240_05_2011-2011.tar.Z";
// where to save it
$output_dir = ".";
// get a temporary file name
$tempfile = sys_get_temp_dir() . basename($url);
// get the file
$compressed_data = file_get_contents($url);
if (empty($compressed_data)) {
echo "error getting $url";
exit;
}
// save it to a local file
$result = file_put_contents($tempfile, $compressed_data);
if (!$result) {
echo "error saving data to $tempfile";
exit;
}
// run the system uncompress utility
exec("/usr/bin/env uncompress $tempfile", $foo, $return);
if ($return == 0) {
// uncompress strips the .Z off the filename
$tempfile = preg_replace("/.Z$/", "", $tempfile);
// remove .tar from the filename for use as a directory
$tempdir = preg_replace("/.tar$/", "", basename($tempfile));
try {
// extract the tar file
$tarchive = new PharData($tempfile);
$tarchive->extractTo("$output_dir/$tempdir");
// loop through the files
$dir = new DirectoryIterator($tempdir);
foreach ($dir as $file) {
if (!$file->isDot()) {
echo $file->getFileName() . "\n";
}
}
} catch (Exception $e) {
echo "Caught exception untarring: " . $e->getMessage();
exit;
}
} else {
echo "uncompress returned error code $return";
exit;
}
答案 3 :(得分:-2)
请试一试。
<?php
try {
$phar = new PharData('myphar.tar');
$phar->extractTo('/full/path'); // extract all files
$phar->extractTo('/another/path', 'file.txt'); // extract only file.txt
$phar->extractTo('/this/path',
array('file1.txt', 'file2.txt')); // extract 2 files only
$phar->extractTo('/third/path', null, true); // extract all files, and overwrite
} catch (Exception $e) {
// handle errors
}
?>
来源:http://php.net/manual/en/phardata.extractto.php 我还没有测试过它,但我希望它对你有用。