解压缩并通过管道传输到新文件时,如何从zip文件末尾删除垃圾?

问题描述 投票:0回答:1

当我在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);
    }
}
windows linux macos unzip
1个回答
1
投票

当涉及到资源派生时, unzip是愚蠢的。 您必须告诉它忽略在.DS_Store找到的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.