{"id":336,"date":"2024-02-12T03:19:10","date_gmt":"2024-02-11T18:19:10","guid":{"rendered":"https:\/\/shugomatsuzawa.com\/techblog\/?p=336"},"modified":"2024-02-12T03:19:12","modified_gmt":"2024-02-11T18:19:12","slug":"laravel%e3%81%a7csv%e3%82%92zip%e3%82%a2%e3%83%bc%e3%82%ab%e3%82%a4%e3%83%96%e3%81%ab%e5%9c%a7%e7%b8%ae%e3%81%97%e3%81%a6%e3%83%80%e3%82%a6%e3%83%b3%e3%83%ad%e3%83%bc%e3%83%89%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/shugomatsuzawa.com\/techblog\/2024\/02\/12\/336\/","title":{"rendered":"Laravel\u3067CSV\u3092Zip\u30a2\u30fc\u30ab\u30a4\u30d6\u306b\u5727\u7e2e\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b"},"content":{"rendered":"\n<p>\u307e\u305a\u914d\u5217\u304b\u3089\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6587\u5b57\u5217\u3092\u4f5c\u308b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private function _createCsvStr()\n{\n    $csvData = array(\n        array(1, 2, 3),\n        array('A', 'B', 'C'),\n    );\n\n    \/\/ \u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6587\u5b57\u5217\u306b\u5909\u63db\n    $response = '';\n    foreach ($csvData as $row) {\n        $response .= implode(',', $row) . \"\\n\"; \/\/ LF\u306a\u3089\\n CRLF\u306a\u3089\\r\\n\n    }\n\n    return $response;\n}<\/code><\/pre>\n\n\n\n<p>\u5b9f\u969b\u306b\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7b49\u304b\u3089\u8aad\u307f\u8fbc\u3093\u3067\u914d\u5217\u3092\u4f5c\u308b\u51e6\u7406\u3092\u66f8\u304f\u3053\u3068\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<p>\u6b21\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308b\u30b3\u30fc\u30c9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private function _createCsvFile($text, $dir, $encoding=null)\n{\n    try {\n        $subdir     = $dir . '\/';\n        $file_name  = date('YmdHis') . '.csv';\n\n        \/\/ \u6587\u5b57\u30b3\u30fc\u30c9\u5909\u63db\uff08\u5fc5\u8981\u306a\u5834\u5408\uff09\n        switch ($encoding) {\n            case null:\n                $encodedData = $text;\n                break;\n            case 'UTF-8BOM':\n                $encodedData = \"\\xEF\\xBB\\xBF\" . $text;\n                break;\n            default:\n                $encodedData = mb_convert_encoding($text, $encoding, 'UTF-8');\n                break;\n        }\n\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\n        Storage::disk('public')->put($subdir.$file_name, $encodedData);\n\n    } catch (\\Exception $e) {\n        Log::error($e->getMessage());\n    }\n    return true;\n}<\/code><\/pre>\n\n\n\n<p>\u6587\u5b57\u30b3\u30fc\u30c9\u304cUTF-8\u3067\u826f\u3044\u306a\u3089\u3001\u5909\u63db\u90e8\u5206\u306f\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u826f\u3044\u3060\u308d\u3046\u3002<\/p>\n\n\n\n<p>\u6b21\u306bZip\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308b\u30b3\u30fc\u30c9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private function _createZip($dir)\n{\n    $target_path    = storage_path('app\/public\/') . $dir;\n    $zip_path       = $target_path . '.zip';\n\n    if (file_exists($target_path)) {\n        $files  = glob($target_path . '\/*');\n        $zip    = new ZipArchive();\n\n        \/\/ \u30d5\u30a1\u30a4\u30eb\u3092Zip\u306b\u8ffd\u52a0\n        $zip->open($zip_path, ZipArchive::CREATE);\n        foreach ($files as $file) {\n            $file_info = pathinfo($file);\n            $file_name = $file_info&#91;'filename'] . '.' . $file_info&#91;'extension'];\n            $zip->addFile($file, $file_name);\n        }\n        $zip->close();\n\n        \/\/ \u6210\u529f\u3057\u305f\u3089\u4f5c\u696d\u30d5\u30a9\u30eb\u30c0\u3092\u524a\u9664\n        if (file_exists($zip_path)) {\n            Storage::disk('public')->deleteDirectory($dir);\n            return $zip_path;\n        }\n    }\n\n    return false;\n}<\/code><\/pre>\n\n\n\n<p><code>use ZipArchive;<\/code>\u304c\u5fc5\u8981\u3002<br>\u7279\u306b\u4f55\u304b\u30d1\u30c3\u30b1\u30fc\u30b8\u7b49\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u5fc5\u8981\u306f\u306a\u3044\u3002<\/p>\n\n\n\n<p>\u6700\u5f8c\u306b\u305d\u308c\u305e\u308c\u306e\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b\u3068\u3053\u308d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function download()\n{\n    $dir_name = date('Ymd');\n\n    \/\/ CSV\u4f5c\u6210\n    $csv_data = $this->_createCsvStr();\n    $this->_createCsvFile($csv_data, $dir_name);\n\n    \/\/ 2\u3064\u3081\u4ee5\u964d\u306e\u30d5\u30a1\u30a4\u30eb\u3082\u8ffd\u52a0\u3057\u3066\u3044\u304f\n    $csv_data2 = $this->_createCsvStr2();\n    $this->_createCsvFile($csv_data, $dir_name);\n\n    \/\/ zip\u4f5c\u6210\n    $zip_path = $file->createZip($dir_name);\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u51fa\u529b\uff08\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\uff09\n    return response()->download($zip_path);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/zenn.dev\/naoki_oshiumi\/articles\/1b6a3b1850fcd8\" data-type=\"link\" data-id=\"https:\/\/zenn.dev\/naoki_oshiumi\/articles\/1b6a3b1850fcd8\" target=\"_blank\" rel=\"noreferrer noopener\">Laravel-Excel\u3067\u4f5c\u6210\u3057\u305fCSV\u3092ZIP\u5316\u3057\u3066\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3059\u308b<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u307e\u305a\u914d\u5217\u304b\u3089\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6587\u5b57\u5217\u3092\u4f5c\u308b\u3002 \u5b9f\u969b\u306b\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7b49\u304b\u3089\u8aad\u307f\u8fbc\u3093\u3067\u914d\u5217\u3092\u4f5c\u308b\u51e6\u7406\u3092\u66f8\u304f\u3053\u3068\u306b\u306a\u308b\u3002 \u6b21\u306bCSV\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u308b\u30b3\u30fc\u30c9\u3002 \u6587\u5b57\u30b3\u30fc\u30c9\u304cUTF-8\u3067\u826f\u3044\u306a\u3089\u3001\u5909\u63db\u90e8\u5206\u306f\u30b9\u30ad\u30c3\u30d7\u3057\u3066\u826f\u3044\u3060\u308d\u3046\u3002 \u6b21\u306b [&hellip;]","protected":false},"author":1,"featured_media":342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[39],"tags":[81,53,10],"class_list":["post-336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backend","tag-laravel","tag-php","tag-memo"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/shugomatsuzawa.com\/techblog\/wp-content\/uploads\/sites\/3\/2024\/02\/zip-7040223_1920.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":3,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":343,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/posts\/336\/revisions\/343"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/media\/342"}],"wp:attachment":[{"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shugomatsuzawa.com\/techblog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}