解压缩并传输到新文件时,如何从 zip 文件末尾删除垃圾?
当我在 Mac 上通过 Excel 输出 .tsv 文件,将其压缩,将其发送到 Linux 计算机,然后使用 unzip 命令解压缩时,我在文件末尾看到一堆垃圾。在该文件中,我有 19 行数据。我使用取景器右键菜单中的默认“压缩”功能。我通过PHP上传文件。以下是我在 zip 文件上运行(手动或自动从脚本)的命令:
unzip -aajp {zipfile} > {newfile}
当我打开 {newfile} 时,我在文件末尾看到所有这些内容:
^@^E^V^G^@^B^@^@Mac OS X ^@^B^@^@^@ ^@^@^@2^@^@^@ ^@^@^@^B^@^@^@R^@^@^@^@TEXTXCEL^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
是否有办法删除末尾的垃圾文件的?
当我运行时:
unzip -aaj {zipfile}
它将解压缩文件,将其转换为文本/纯文本,没有垃圾就好了。但在我的 PHP 脚本中,我需要能够获取文件的确切名称/位置。
无论哪种方式,我都愿意这样做。我似乎找不到正确的解决方案。话虽这么说,它也需要适用于来自 Windows 的文件。有什么想法吗?
更新:
这是我最终所做的,但仍然感觉很草率。我仍然愿意接受更好的解决方案。
function decompress($filename) {
// generate a temporary filename
$tmpfile = '/tmp/'.mt_rand();
// Here we actually decompress the $working_zip_file file
$command = "unzip -aao $filename -d $tmpfile/ | egrep \"(inflating:|extracting:)\" | grep -v MACOS ";
$unzip_output = exec($command, $dummy, $unzipstatus);
// If things where unzipped properly
if($unzipstatus[0] == 0) {
$work_plain_file = preg_match('/\s*(inflating:|extracting:)(.*)$/', $unzip_output, $matches);
$work_plain_file = trim($matches[2]);
$clean_name = str_replace(' ', '_', $work_plain_file);
if($clean_name != $work_plain_file){
exec("mv \"$work_plain_file\" $clean_name");
$work_plain_file = $clean_name;
}
rename($work_plain_file, $new_file);
}
}
When I take a .tsv file ouput by Excel on a Mac, zip it, send it to a linux machine, and unzip it using the unzip command, I get a bunch of junk on the end of the file. In the file, I have 19 rows of data. I use the default "Compress" function from the right click menu in finder. I upload the file through PHP. Here is the command I run (manually or automatically from the script) on the zip file:
unzip -aajp {zipfile} > {newfile}
When I open the {newfile} I see all of this on the end of the file:
^@^E^V^G^@^B^@^@Mac OS X ^@^B^@^@^@ ^@^@^@2^@^@^@ ^@^@^@^B^@^@^@R^@^@^@^@TEXTXCEL^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
Is there anyway to get rid of the junk on the end of the file?
When I run:
unzip -aaj {zipfile}
It will unzip the file, converting it to text/plain without the junk just fine. But then within my PHP script, I need to be able to get the exact name/location of the file.
I am open to doing this either way. I just cannot seem to find the correct solution. That being said, it needs to work for a file coming from windows as well. Any ideas?
UPDATE:
Here is what I ended up doing, but it still feels sloppy. I am still open to a better solution.
function decompress($filename) {
// generate a temporary filename
$tmpfile = '/tmp/'.mt_rand();
// Here we actually decompress the $working_zip_file file
$command = "unzip -aao $filename -d $tmpfile/ | egrep \"(inflating:|extracting:)\" | grep -v MACOS ";
$unzip_output = exec($command, $dummy, $unzipstatus);
// If things where unzipped properly
if($unzipstatus[0] == 0) {
$work_plain_file = preg_match('/\s*(inflating:|extracting:)(.*)$/', $unzip_output, $matches);
$work_plain_file = trim($matches[2]);
$clean_name = str_replace(' ', '_', $work_plain_file);
if($clean_name != $work_plain_file){
exec("mv \"$work_plain_file\" $clean_name");
$work_plain_file = $clean_name;
}
rename($work_plain_file, $new_file);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
unzip
对于资源分支来说是愚蠢的。您必须告诉它忽略在.DS_Store
中找到的任何内容。unzip
is dumb when it comes to the resource fork. You must tell it to ignore anything it finds in.DS_Store
.