回答編集履歴

7

修正

2021/09/15 07:01

投稿

退会済みユーザー
test CHANGED
@@ -30,6 +30,10 @@
30
30
 
31
31
  こちらで動作確認したコードを載せておきます。日本語のフォルダ名やファイル名でも、特に問題ありませんでした。
32
32
 
33
+ [2021/09/15 15:51]
34
+
35
+ Shift_JISファイル名のZIP出力処理を追記しました。
36
+
33
37
  ```vba
34
38
 
35
39
  Option Explicit
@@ -47,24 +51,6 @@
47
51
  ByVal lpParameters As LongPtr, _
48
52
 
49
53
  ByVal lpDirectory As LongPtr, _
50
-
51
- ByVal nShowCmd As Long _
52
-
53
- ) As LongPtr
54
-
55
-
56
-
57
- Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
58
-
59
- ByVal hwnd As LongPtr, _
60
-
61
- ByVal lpOperation As String, _
62
-
63
- ByVal lpFile As String, _
64
-
65
- ByVal lpParameters As String, _
66
-
67
- ByVal lpDirectory As String, _
68
54
 
69
55
  ByVal nShowCmd As Long _
70
56
 
@@ -154,15 +140,7 @@
154
140
 
155
141
  End Sub
156
142
 
157
- ```
158
143
 
159
- ---
160
-
161
- [2021/09/15 15:51]
162
-
163
- Shift_JISファイル名のZIP出力処理を追記しました。
164
-
165
- ```vba
166
144
 
167
145
  'ShellExecuteWで実行(Shift_JIS出力)
168
146
 

6

Shift_JISファイル名のZIP出力処理を追記

2021/09/15 07:00

投稿

退会済みユーザー
test CHANGED
@@ -32,6 +32,10 @@
32
32
 
33
33
  ```vba
34
34
 
35
+ Option Explicit
36
+
37
+
38
+
35
39
  Private Declare PtrSafe Function ShellExecuteW Lib "shell32.dll" ( _
36
40
 
37
41
  ByVal hwnd As LongPtr, _
@@ -43,6 +47,24 @@
43
47
  ByVal lpParameters As LongPtr, _
44
48
 
45
49
  ByVal lpDirectory As LongPtr, _
50
+
51
+ ByVal nShowCmd As Long _
52
+
53
+ ) As LongPtr
54
+
55
+
56
+
57
+ Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
58
+
59
+ ByVal hwnd As LongPtr, _
60
+
61
+ ByVal lpOperation As String, _
62
+
63
+ ByVal lpFile As String, _
64
+
65
+ ByVal lpParameters As String, _
66
+
67
+ ByVal lpDirectory As String, _
46
68
 
47
69
  ByVal nShowCmd As Long _
48
70
 
@@ -71,6 +93,8 @@
71
93
  Call CreateZipWsh(src, "c:\test\test_wsh.zip")
72
94
 
73
95
  Call CreateZipShellExecuteW(src, "c:\test\test_shellw.zip")
96
+
97
+ Call CreateZipShellExecuteW_SJIS(src, "c:\test\test_shellw_sjis.zip")
74
98
 
75
99
  End Sub
76
100
 
@@ -124,10 +148,48 @@
124
148
 
125
149
  exe = "powershell.exe"
126
150
 
127
- parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path " & src & " -DestinationPath " & dest & " -Force"
151
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force"
128
152
 
129
153
  Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
130
154
 
131
155
  End Sub
132
156
 
133
157
  ```
158
+
159
+ ---
160
+
161
+ [2021/09/15 15:51]
162
+
163
+ Shift_JISファイル名のZIP出力処理を追記しました。
164
+
165
+ ```vba
166
+
167
+ 'ShellExecuteWで実行(Shift_JIS出力)
168
+
169
+ Private Sub CreateZipShellExecuteW_SJIS(ByVal src As String, ByVal dest As String)
170
+
171
+ Dim exe As String
172
+
173
+ Dim operation As String
174
+
175
+ Dim cmd As String
176
+
177
+ Dim parameter As String
178
+
179
+
180
+
181
+ operation = "open"
182
+
183
+ exe = "powershell.exe"
184
+
185
+ cmd = "Add-Type -AssemblyName System.IO.Compression.FileSystem;"
186
+
187
+ cmd = cmd & "[IO.Compression.ZipFile]::CreateFromDirectory('" & src & "', '" & dest & "', [IO.Compression.CompressionLevel]::Optimal, $true, [Text.Encoding]::GetEncoding('Shift_JIS'));"
188
+
189
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command " & cmd
190
+
191
+ Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
192
+
193
+ End Sub
194
+
195
+ ```

5

不要コード除去

2021/09/15 06:58

投稿

退会済みユーザー
test CHANGED
@@ -120,11 +120,11 @@
120
120
 
121
121
 
122
122
 
123
- operation = "open" & vbNullChar & vbNullChar
123
+ operation = "open"
124
124
 
125
- exe = "powershell.exe" & vbNullChar & vbNullChar
125
+ exe = "powershell.exe"
126
126
 
127
- parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force" & vbNullChar & vbNullChar
127
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path " & src & " -DestinationPath " & dest & " -Force"
128
128
 
129
129
  Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
130
130
 

4

サンプル追記

2021/09/15 00:44

投稿

退会済みユーザー
test CHANGED
@@ -25,3 +25,109 @@
25
25
  [これで解消!「KB2704299」でCompress-Archiveの文字化け対処](https://cheshire-wara.com/powershell/ps-column/compress-archive-resolved/)
26
26
 
27
27
  こんな記事がありましたが、上記環境に該当してないですか?
28
+
29
+
30
+
31
+ こちらで動作確認したコードを載せておきます。日本語のフォルダ名やファイル名でも、特に問題ありませんでした。
32
+
33
+ ```vba
34
+
35
+ Private Declare PtrSafe Function ShellExecuteW Lib "shell32.dll" ( _
36
+
37
+ ByVal hwnd As LongPtr, _
38
+
39
+ ByVal lpOperation As LongPtr, _
40
+
41
+ ByVal lpFile As LongPtr, _
42
+
43
+ ByVal lpParameters As LongPtr, _
44
+
45
+ ByVal lpDirectory As LongPtr, _
46
+
47
+ ByVal nShowCmd As Long _
48
+
49
+ ) As LongPtr
50
+
51
+
52
+
53
+ Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)
54
+
55
+
56
+
57
+ Const SW_HIDE = 0 'ウィンドウを表示しない
58
+
59
+ Const SW_SHOW = 5 'ウィンドウを現在の位置とサイズで表示
60
+
61
+
62
+
63
+ Private Sub CommandButton1_Click()
64
+
65
+ Dim src As String
66
+
67
+ src = "c:\test\あいうえお"
68
+
69
+
70
+
71
+ Call CreateZipWsh(src, "c:\test\test_wsh.zip")
72
+
73
+ Call CreateZipShellExecuteW(src, "c:\test\test_shellw.zip")
74
+
75
+ End Sub
76
+
77
+
78
+
79
+ 'WScript.Shellで実行
80
+
81
+ Private Sub CreateZipWsh(ByVal src As String, ByVal dest As String)
82
+
83
+ Dim parameter As String
84
+
85
+ Dim oWsh As Object
86
+
87
+ Dim oExec As Object
88
+
89
+
90
+
91
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force"
92
+
93
+ Set oWsh = CreateObject("WScript.Shell")
94
+
95
+ Set oExec = oWsh.Exec("powershell.exe" & " " & parameter)
96
+
97
+ Do While oExec.Status = 0
98
+
99
+ Sleep 1
100
+
101
+ Loop
102
+
103
+ Set oExec = Nothing
104
+
105
+ Set oWsh = Nothing
106
+
107
+ End Sub
108
+
109
+
110
+
111
+ 'ShellExecuteWで実行
112
+
113
+ Private Sub CreateZipShellExecuteW(ByVal src As String, ByVal dest As String)
114
+
115
+ Dim exe As String
116
+
117
+ Dim operation As String
118
+
119
+ Dim parameter As String
120
+
121
+
122
+
123
+ operation = "open" & vbNullChar & vbNullChar
124
+
125
+ exe = "powershell.exe" & vbNullChar & vbNullChar
126
+
127
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force" & vbNullChar & vbNullChar
128
+
129
+ Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
130
+
131
+ End Sub
132
+
133
+ ```

3

追記

2021/09/14 08:43

投稿

退会済みユーザー
test CHANGED
@@ -6,7 +6,9 @@
6
6
 
7
7
  考えられる解決策としては、
8
8
 
9
- - 何とかしてPowerShellにファイル名を渡す(PowerShellのスクリプトファイルを出力して実行するとか?)
9
+ - ~~何とかしてPowerShellにファイル名を渡す(PowerShellのスクリプトファイルを出力して実行するとか?)~~
10
+
11
+ エンコードせずにpowershellコマンドに渡して、文字化けしない事を確認しました。(Windows10)
10
12
 
11
13
  - Compress-Archiveの代替手段を探す
12
14
 
@@ -17,3 +19,9 @@
17
19
 
18
20
 
19
21
  こんなもんでしょうか。
22
+
23
+
24
+
25
+ [これで解消!「KB2704299」でCompress-Archiveの文字化け対処](https://cheshire-wara.com/powershell/ps-column/compress-archive-resolved/)
26
+
27
+ こんな記事がありましたが、上記環境に該当してないですか?

2

追記

2021/09/14 05:23

投稿

退会済みユーザー
test CHANGED
@@ -1,3 +1,19 @@
1
1
  解凍時の文字化けは、ZIPファイルのヘッダか、解凍ソフトのどちらかに問題があると思われます。ZIPのヘッダには、utf-8を使用しているかのビットフラグが存在し、そこを見て解凍ソフト側が文字コード処理を切り替えるように実装しているかどうかです。7-Zip辺りは、Shift_JISでファイル名を突っ込んでも、そのビットがOFFなら正しいファイル名で解凍してくれると思いますが。
2
2
 
3
3
  割と根が深い問題なので、ヘッダをアテにせず、独自に文字コード判定を行っている解凍ソフトもあります。
4
+
5
+
6
+
7
+ 考えられる解決策としては、
8
+
9
+ - 何とかしてPowerShellにファイル名を渡す(PowerShellのスクリプトファイルを出力して実行するとか?)
10
+
11
+ - Compress-Archiveの代替手段を探す
12
+
13
+ - 出力されたZIPを何らかの手段で修復・変換する
14
+
15
+ (ヘッダがおかしいか、ファイル名がおかしいかを特定する必要あり)
16
+
17
+
18
+
19
+ こんなもんでしょうか。

1

修正

2021/09/14 02:59

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,3 @@
1
- 解凍時の文字化けは、ZIPファイルのヘッダか、解凍ソフトのどちらかに問題があると思われます。ZIPのヘッダには、utf-8を使用しているかのビットフラグが存在し、そこを見て解凍ソフト側が処理を切り替えるように実装しているかどうかです。7-Zip辺りは、Shift_JISでファイル名を突っ込んでも、そのビットがOFFなら正しいファイル名で解凍してくれると思いますが。
1
+ 解凍時の文字化けは、ZIPファイルのヘッダか、解凍ソフトのどちらかに問題があると思われます。ZIPのヘッダには、utf-8を使用しているかのビットフラグが存在し、そこを見て解凍ソフト側が文字コード処理を切り替えるように実装しているかどうかです。7-Zip辺りは、Shift_JISでファイル名を突っ込んでも、そのビットがOFFなら正しいファイル名で解凍してくれると思いますが。
2
+
3
+ 割と根が深い問題なので、ヘッダをアテにせず、独自に文字コード判定を行っている解凍ソフトもあります。