使用Zip php创建ODT文件时出现一般输入/输出错误。

7
我的系统生成一个包含下列文件和子文件夹的文件夹。
我正在使用这段代码来创建一个odt文件。当我在Apache + PHP和Ubuntu 12.04上运行我的系统时,使用此代码将子文件夹和文件压缩到test.odt文件中,odt文件是正常的。但是,当我在IIS + PHP和Windows上将其压缩到test.odt文件中时,odt文件无法正常工作。我知道它不能正常工作,因为当我用LibreOffice打开odt文件时,我得到了输入/输出一般错误的错误。这里有odt文件。
当我解压在Windows上生成的odt文件时,文件夹和文件的形式与图片相同。我在谷歌上查找,也许问题是mimetype编码。
我该如何修改代码1以解决我的问题?
编辑
我使用这个文件夹和下面的代码,但我只在我的odt文件中得到了"PKÜNTEBasic/PKÜNTEúミ‚lÓR/Basic/script-lc.xmleマAoÂ0"。 这里是示例
<?php

$wordtemplatedownloadpath = "siscons\\test\\wordtemplatedownload\\";
Zip($wordtemplatedownloadpath . "pasta3", $wordtemplatedownloadpath . "test.odt");
force_download("test.odt", $wordtemplatedownloadpath . "test.odt");
//echo "<a href=test/wordtemplatedownload/test.odt>" . "Download Force". "</a>";
function Zip($source, $destination)
{
    if (!extension_loaded('zip') || !file_exists($source)) {
        return false;
    }

    $zip = new ZipArchive();
    if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
        return false;
    }

    $source = str_replace('\\', '/', realpath($source));
    //echo $source;
    if (is_dir($source) === true) {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

        foreach ($files as $file) {
            //print "file#".$file."\n";
            $file = str_replace('\\', '/', $file);

            // Ignore "." and ".." folders
            if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
                continue;

            //$file = realpath($file);
            //print "file#".$file."\n";
            if (is_dir($file) === true) {
                //$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                //echo "sourcE".$source."FilE".$file."ReplacE".str_replace($source . '/', '', $file)."End";
                $zip->addEmptyDir(str_replace($source . '/', '', $file));
            } else if (is_file($file) === true) {

                $str1 = str_replace($source . '/', '', '/' . $file);
                $str1 = str_replace('/', '/', $str1);
                $zip->addFromString($str1, file_get_contents($file));
                //$zip->addFromString("subfolder/styles.xml", file_get_contents($file));

            }
        }
    } else if (is_file($source) === true) {
        $zip->addFromString(basename($source), file_get_contents($source));
    }

    return $zip->close();
}

function force_download($filename = '', $data = '')
{
    if ($filename == '' || $data == '') {
        return false;
    }

    if (!file_exists($data)) {
        return false;
    }

    // Try to determine if the filename includes a file extension.
    // We need it in order to set the MIME type
    if (false === strpos($filename, '.')) {
        return false;
    }

    // Grab the file extension
    $extension = strtolower(pathinfo(basename($filename), PATHINFO_EXTENSION));

    // our list of mime types
    $mime_types = array(

        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',
        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',

        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    // Set a default mime if we can't find it
    if (!isset($mime_types[$extension])) {
        $mime = 'application/octet-stream';
    } else {
        $mime = (is_array($mime_types[$extension])) ? $mime_types[$extension][0] : $mime_types[$extension];
    }

    // Generate the server headers
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: " . filesize($data));
    } else {
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: private", false);
        header("Content-Type: " . $mime, true, 200);
        header('Content-Length: ' . filesize($data));
        header('Content-Disposition: attachment; filename=' . $filename);
        header("Content-Transfer-Encoding: binary");
    }
    readfile($data);
    exit;

} //End force_download
?>

2
请添加收到的错误信息和一个下载受影响的ODT文件的链接。谢谢。 - dotancohen
我根据你的建议编辑了我的问题。 - Juan
我使用 PClZIP 和 http://www.phpconcept.net/pclzip/faq 中的 Q5,现在我的脚本可以运行了! - Juan
1
太好了!我建议您将其添加为“答案”,然后接受自己的答案。这在SO上实际上是鼓励的,因为它有助于未来遇到该问题的其他人。 - dotancohen
找不到ODT文件。你能再上传一次吗? - Abrar Jahin
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接