标签: ziparchive

使用System.IO.Compression在内存中创建ZIP存档

我正在尝试使用MemoryStream如下的简单演示文本文件创建ZIP存档:

using (var memoryStream = new MemoryStream())
using (var archive = new ZipArchive(memoryStream , ZipArchiveMode.Create))
{
    var demoFile = archive.CreateEntry("foo.txt");

    using (var entryStream = demoFile.Open())
    using (var streamWriter = new StreamWriter(entryStream))
    {
        streamWriter.Write("Bar!");
    }

    using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
    {
        stream.CopyTo(fileStream);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我运行此代码,则会创建存档文件本身,但不会创建foo.txt.

但是,如果我MemoryStream直接替换文件流,则会正确创建存档:

using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
using (var archive = new ZipArchive(fileStream, FileMode.Create))
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

是否可以使用a MemoryStream创建ZIP存档而不用FileStream

.net c# compression zip ziparchive

163
推荐指数
5
解决办法
15万
查看次数

致命错误:找不到类"ZipArchive"

我有一个问题,我在Linux服务器上安装'Archive_Zip 0.1.1',但是当我尝试运行脚本来创建zip文件时,它会给出致命的错误

致命错误:ZipArchive在...中找不到类

我把代码放在哪里

$zip = new ZipArchive;
var_dump($zip);
$res = $zip->open($filename, ZipArchive::OVERWRITE);
if ($res !== TRUE) {
    echo 'Error: Unable to create zip file';
    exit;
}
if (is_file($src)) {
    $zip->addFile($src);
} else {
    // echo "<br>" . dirname(__FILE__) . $src;//'/install1';
    if (!is_dir($src)) {
         $zip->close();
         @unlink($filename);
         echo 'Error: File not found';
         exit;
    }
    recurse_zip($src, $zip, $path_length);
}
$zip->close();
echo "<br>file name ".$filename;
Run Code Online (Sandbox Code Playgroud)

但它找不到类文件.

请告诉我解决方案.我该怎么做才能解决问题?我也把php.ini文件放到脚本所在的文件夹中,但它不起作用.

php zip ziparchive

153
推荐指数
11
解决办法
26万
查看次数

ZipArchive创建无效的ZIP文件

我试图从一个条目的代码创建一个新的ZIP包,并将ZIP包保存到文件.我试图用System.IO.Compression.ZipArchive类来实现这一点.我正在使用以下代码创建ZIP包:

using (MemoryStream zipStream = new MemoryStream())
{
    using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
    {
        var entry = zip.CreateEntry("test.txt");
        using (StreamWriter sw = new StreamWriter(entry.Open()))
        {
            sw.WriteLine(
                "Etiam eros nunc, hendrerit nec malesuada vitae, pretium at ligula.");
        }
Run Code Online (Sandbox Code Playgroud)

然后我将ZIP保存到WinRT中的文件:

        var file = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("test.zip", CreationCollisionOption.ReplaceExisting);
        zipStream.Position = 0;
        using (Stream s = await file.OpenStreamForWriteAsync())
        {
            zipStream.CopyTo(s);
        }
Run Code Online (Sandbox Code Playgroud)

或者在普通的.NET 4.5中:

        using (FileStream fs = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
        {
            zipStream.Position = 0;
            zipStream.CopyTo(fs);
        }
Run Code Online (Sandbox Code Playgroud)

但是,我无法在Windows资源管理器,WinRAR等中打开生成的文件.(我检查生成的文件的大小是否与zipStream的长度匹配,因此流本身已正确保存到文件中.)
我是做错了什么或者ZipArchive类有问题吗?

.net c# ziparchive windows-8

45
推荐指数
2
解决办法
4万
查看次数

压缩目录中的所有文件并下载生成的.zip

好吧,首先,这是我的文件夹结构:

images/

image1.png
image11.png
image111.png
image223.png
generate_zip.php
Run Code Online (Sandbox Code Playgroud)

这是我的generate_zip.php:

<?php

    $files = array($listfiles);

    $zipname = 'adcs.zip';
    $zip = new ZipArchive;
    $zip->open($zipname, ZipArchive::CREATE);
    foreach ($files as $file) {
      $zip->addFile($file);
    }
    $zip->close();

    header('Content-Type: application/zip');
    header("Content-Disposition: attachment; filename='adcs.zip'");
    header('Content-Length: ' . filesize($zipname));
    header("Location: adcs.zip");

    ?>
Run Code Online (Sandbox Code Playgroud)

如何收集"images /"文件夹中的所有文件,"generate_zip.php"除外,并使其成为可下载的.zip文件?在这种情况下,"images /"文件夹始终具有不同的图像.那可能吗?

php directory zip ziparchive

29
推荐指数
2
解决办法
8万
查看次数

用ziparchive覆盖文件

我有archive.zip两个文件:hello.txtworld.txt

我想hello.txt使用该代码覆盖新文件:

import zipfile

z = zipfile.ZipFile('archive.zip','a')
z.write('hello.txt')
z.close()  
Run Code Online (Sandbox Code Playgroud)

但它不会覆盖文件,不知何故它会创建另一个实例hello.txt- 看看winzip截图:

替代文字

既然没有smth zipfile.remove(),那么处理这个问题的最佳方法是什么?

python ziparchive

27
推荐指数
2
解决办法
1万
查看次数

PHPExcel_Writer_Exception,消息"无法关闭zip文件php://输出."

我正在使用PHPExcel将一些数据导出到excel文件中的用户.我希望脚本在创建后立即将excel文件发送给用户.这是我的测试代码:

try{

  /* Some test data */
  $data = array(
    array(1, 10   , 2             ,),
    array(3, 'qqq', 'some string' ,),
  );

  $objPHPExcel = new PHPExcel();
  $objPHPExcel->setActiveSheetIndex(0);

  /* Fill the excel sheet with the data */
  $rowI = 0;
  foreach($data as $row){
    $colI = 0;
    foreach($row as $v){
      $colChar = PHPExcel_Cell::stringFromColumnIndex($colI++);
      $cellId = $colChar.($rowI+1);
      $objPHPExcel->getActiveSheet()->SetCellValue($cellId, $v);
    }
    $rowI++;
  }

  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="export.xlsx"');
  header('Cache-Control: max-age=0');

  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  $objWriter->save('php://output');

}catch(Exception $e){
  echo $e->__toString();
}
Run Code Online (Sandbox Code Playgroud)

在我的本地服务器上,(Windows 7 x64, Php 5.3.8, …

php phpexcel ziparchive

26
推荐指数
3
解决办法
6万
查看次数

使用PHP类ZipArchive创建一个zip文件而不将文件写入磁盘?

我想使用ZipArchive(或本机PHP类)在内存中创建一个zip文件,并将文件内容读回客户端.这可能吗?如果是这样,怎么样?

我想在此应用程序中压缩的文件总共最多15 MB.我认为我们应该记忆良好.

php ziparchive

22
推荐指数
3
解决办法
2万
查看次数

PHP ZipArchive在Windows中损坏

我使用PHP的ZipArchive类创建一个包含照片的zip文件,然后将其提供给浏览器下载.这是我的代码:

/**
 * Grabs the order, packages the files, and serves them up for download.
 *
 * @param string $intEntryID 
 * @return void
 * @author Jesse Bunch
 */
public static function download_order_by_entry_id($intUniqueID) {

    $objCustomer = PhotoCustomer::get_customer_by_unique_id($intUniqueID);

    if ($objCustomer):

        if (!class_exists('ZipArchive')):
            trigger_error('ZipArchive Class does not exist', E_USER_ERROR);
        endif;

        $objZip = new ZipArchive();
        $strZipFilename = sprintf('%s/application/tmp/%s-%s.zip', $_SERVER['DOCUMENT_ROOT'], $objCustomer->getEntryID(), time());

        if ($objZip->open($strZipFilename, ZIPARCHIVE::CREATE) !== TRUE):

            trigger_error('Unable to create zip archive', E_USER_ERROR);

        endif;          

        foreach($objCustomer->arrPhotosRequested as $objPhoto):

            $filename = PhotoCart::replace_ee_file_dir_in_string($objPhoto->strHighRes);
            $objZip->addFile($filename,sprintf('/press_photos/%s-%s', $objPhoto->getEntryID(), basename($filename)));

        endforeach;

        $objZip->close(); …
Run Code Online (Sandbox Code Playgroud)

php windows ziparchive

19
推荐指数
5
解决办法
2万
查看次数

Composer安装错误:ZipArchive :: extractTo():完全提取路径超过MAXPATHLEN(260)

当我跑:

php composer.phar require kartik-v/yii2-widgets "*"
Run Code Online (Sandbox Code Playgroud)

我得到以下输出(使用-vvv verbose标志):

  - Installing kartik-v/bootstrap-fileinput (v4.1.7)
Downloading https://api.github.com/repos/kartik-v/bootstrap-fileinput/zipball/f95a7e5fa0a9db1ead445e438653aa71e9f599f9
    Downloading: connection...
    Downloading: 0%
    Downloading: 5%
    ...
    Downloading: 95%
    Downloading: 100%
    Downloading: 100%
Writing C:/Users/Michael/AppData/Local/Composer/files/kartik-v/bootstrap-fileinput/f95a7e5fa0a9db1ead445e438653aa71e9f599f9.zip into cache
    Extracting archive

  [ErrorException]
  ZipArchive::extractTo(): Full extraction path exceed MAXPATHLEN (260)

Exception trace:
 () at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Downloader/ZipDownloader.php:79
 Composer\Util\ErrorHandler::handle() at n/a:n/a
 ZipArchive->extractTo() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Downloader/ZipDownloader.php:79
 Composer\Downloader\ZipDownloader->extract() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Downloader/ArchiveDownloader.php:44
 Composer\Downloader\ArchiveDownloader->download() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Downloader/DownloadManager.php:201
 Composer\Downloader\DownloadManager->download() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer/LibraryInstaller.php:156
 Composer\Installer\LibraryInstaller->installCode() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer/LibraryInstaller.php:87
 Composer\Installer\LibraryInstaller->install() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer/InstallationManager.php:152
 Composer\Installer\InstallationManager->install() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer/InstallationManager.php:139
 Composer\Installer\InstallationManager->execute() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer.php:578
 Composer\Installer->doInstall() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Installer.php:225
 Composer\Installer->run() at phar://C:/ProgramData/ComposerSetup/bin/composer.phar/src/Composer/Command/RequireCommand.php:154
 Composer\Command\RequireCommand->execute() …
Run Code Online (Sandbox Code Playgroud)

install require ziparchive composer-php yii2

15
推荐指数
3
解决办法
9883
查看次数

在PHP中将提取的Zip文件重命名为其他语言时出错

我使用PHP ZipArchive类来提取.zip文件,它适用于英语,但导致我的本地语言(THAI)出现问题.

icov('utf-8','windows-874',$zip->getNameIndex($i))用来将utf-8转换为THAI.它适用于文件夹/文件的名称,但不适用于解压缩的.zip文件并导致此错误:

iconv():检测到输入字符串中的非法字符

谁能告诉我这里的问题是什么?

我的PHP代码

$file = iconv('utf-8', 'windows-874', $_GET['File']);
$path = iconv('utf-8', 'windows-874', $_GET['Path']);

$zip = new ZipArchive;
if ($zip->open($file) === TRUE) {
    // convert to Thai language
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        //echo iconv("charset zip file", "windows-874", $name);
        //$zip->extractTo($path,$name); -> this problem
    }
    $zip->close();
    echo json_encode('unZip!!!');
} else {
    echo json_encode('Failed');
}
Run Code Online (Sandbox Code Playgroud)

解压缩压缩文件后,文件的名称不是我为其设置的名称. 解压缩压缩文件后,文件的名称不是我为其设置的名称.

这是我试图设置的名称: 这是我试图设置的名称:

这是我的压缩文件:

https://www.dropbox.com/s/9f4j04lkvsyuy63/test.zip?dl=0

更新
我尝试在Windows XP中解压缩文件,它在那里工作正常,但在Windows 7中没有.

php unicode renaming ziparchive thai

13
推荐指数
1
解决办法
1192
查看次数