MochiuWiki : SUSE, EC, PCB
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
検索
個人用ツール
ログイン
Toggle dark mode
名前空間
ページ
議論
表示
閲覧
ソースを閲覧
履歴を表示
PHPの基礎 - 圧縮・解凍のソースを表示
提供: MochiuWiki : SUSE, EC, PCB
←
PHPの基礎 - 圧縮・解凍
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループのいずれかに属する利用者のみが実行できます:
管理者
、new-group。
このページのソースの閲覧やコピーができます。
== 概要 == <br><br> == 圧縮 == ==== ZIP形式 ==== <syntaxhighlight lang="php"> declare(strict_types=1); namespace Utils; use RuntimeException; use ZipArchive; use Generator; /** * ZIP形式で圧縮を行うメソッド * * @param string $sourcePath 圧縮対象のパス(ファイルまたはディレクトリ) * @param string $destinationPath 出力先のZIPファイルパス * @throws RuntimeException 圧縮処理中のエラー発生時 */ function compressToZip(string $sourcePath, string $destinationPath): void { try { $zip = new ZipArchive(); // ZIPファイルのオープン if ($zip->open($destinationPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) { throw new RuntimeException('ZIPファイルの作成に失敗しました'); } // ディレクトリの場合は再帰的に処理 if (is_dir($sourcePath)) { $files = $this->getFilesRecursively($sourcePath); foreach ($files as $file) { $relativePath = substr($file, strlen($sourcePath) + 1); $zip->addFile($file, $relativePath); } } else { // 単一ファイルの場合 $zip->addFile($sourcePath, basename($sourcePath)); } $zip->close(); } catch (\Exception $e) { throw new RuntimeException("ZIP圧縮処理中にエラーが発生しました: {$e->getMessage()}"); } } /** * ディレクトリ内のファイルを再帰的に取得するプライベートメソッド * * @param string $dir 検索対象ディレクトリ * @return Generator ファイルパスを返すジェネレータ */ function getFilesRecursively(string $dir): Generator { $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY); foreach ($files as $file) { yield $file->getRealPath(); } } </syntaxhighlight> <br> <syntaxhighlight lang="php"> // 使用例 $compression = new Utils\CompressionUtils(); try { $compression->compressToZip('/path/to/source', '/path/to/output.zip'); } catch (RuntimeException $e) { echo "エラーが発生しました: " . $e->getMessage(); } </syntaxhighlight> <br><br> {{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux |image=/resources/assets/MochiuLogo_Single_Blue.png }} __FORCETOC__ [[カテゴリ:Web]]
PHPの基礎 - 圧縮・解凍
に戻る。
案内
メインページ
最近の更新
おまかせ表示
MediaWiki についてのヘルプ
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
We ask for
Donations
Collapse