teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

7

修正

2021/09/15 07:01

投稿

退会済みユーザー
answer CHANGED
@@ -14,6 +14,8 @@
14
14
  こんな記事がありましたが、上記環境に該当してないですか?
15
15
 
16
16
  こちらで動作確認したコードを載せておきます。日本語のフォルダ名やファイル名でも、特に問題ありませんでした。
17
+ [2021/09/15 15:51]
18
+ Shift_JISファイル名のZIP出力処理を追記しました。
17
19
  ```vba
18
20
  Option Explicit
19
21
 
@@ -26,15 +28,6 @@
26
28
  ByVal nShowCmd As Long _
27
29
  ) As LongPtr
28
30
 
29
- Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
30
- ByVal hwnd As LongPtr, _
31
- ByVal lpOperation As String, _
32
- ByVal lpFile As String, _
33
- ByVal lpParameters As String, _
34
- ByVal lpDirectory As String, _
35
- ByVal nShowCmd As Long _
36
- ) As LongPtr
37
-
38
31
  Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)
39
32
 
40
33
  Const SW_HIDE = 0 'ウィンドウを表示しない
@@ -76,11 +69,7 @@
76
69
  parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force"
77
70
  Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
78
71
  End Sub
79
- ```
72
+
80
- ---
81
- [2021/09/15 15:51]
82
- Shift_JISファイル名のZIP出力処理を追記しました。
83
- ```vba
84
73
  'ShellExecuteWで実行(Shift_JIS出力)
85
74
  Private Sub CreateZipShellExecuteW_SJIS(ByVal src As String, ByVal dest As String)
86
75
  Dim exe As String

6

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

2021/09/15 07:00

投稿

退会済みユーザー
answer CHANGED
@@ -15,6 +15,8 @@
15
15
 
16
16
  こちらで動作確認したコードを載せておきます。日本語のフォルダ名やファイル名でも、特に問題ありませんでした。
17
17
  ```vba
18
+ Option Explicit
19
+
18
20
  Private Declare PtrSafe Function ShellExecuteW Lib "shell32.dll" ( _
19
21
  ByVal hwnd As LongPtr, _
20
22
  ByVal lpOperation As LongPtr, _
@@ -24,6 +26,15 @@
24
26
  ByVal nShowCmd As Long _
25
27
  ) As LongPtr
26
28
 
29
+ Private Declare PtrSafe Function ShellExecuteA Lib "shell32.dll" ( _
30
+ ByVal hwnd As LongPtr, _
31
+ ByVal lpOperation As String, _
32
+ ByVal lpFile As String, _
33
+ ByVal lpParameters As String, _
34
+ ByVal lpDirectory As String, _
35
+ ByVal nShowCmd As Long _
36
+ ) As LongPtr
37
+
27
38
  Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)
28
39
 
29
40
  Const SW_HIDE = 0 'ウィンドウを表示しない
@@ -35,6 +46,7 @@
35
46
 
36
47
  Call CreateZipWsh(src, "c:\test\test_wsh.zip")
37
48
  Call CreateZipShellExecuteW(src, "c:\test\test_shellw.zip")
49
+ Call CreateZipShellExecuteW_SJIS(src, "c:\test\test_shellw_sjis.zip")
38
50
  End Sub
39
51
 
40
52
  'WScript.Shellで実行
@@ -61,7 +73,26 @@
61
73
 
62
74
  operation = "open"
63
75
  exe = "powershell.exe"
64
- parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path " & src & " -DestinationPath " & dest & " -Force"
76
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force"
65
77
  Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
66
78
  End Sub
79
+ ```
80
+ ---
81
+ [2021/09/15 15:51]
82
+ Shift_JISファイル名のZIP出力処理を追記しました。
83
+ ```vba
84
+ 'ShellExecuteWで実行(Shift_JIS出力)
85
+ Private Sub CreateZipShellExecuteW_SJIS(ByVal src As String, ByVal dest As String)
86
+ Dim exe As String
87
+ Dim operation As String
88
+ Dim cmd As String
89
+ Dim parameter As String
90
+
91
+ operation = "open"
92
+ exe = "powershell.exe"
93
+ cmd = "Add-Type -AssemblyName System.IO.Compression.FileSystem;"
94
+ cmd = cmd & "[IO.Compression.ZipFile]::CreateFromDirectory('" & src & "', '" & dest & "', [IO.Compression.CompressionLevel]::Optimal, $true, [Text.Encoding]::GetEncoding('Shift_JIS'));"
95
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command " & cmd
96
+ Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
97
+ End Sub
67
98
  ```

5

不要コード除去

2021/09/15 06:58

投稿

退会済みユーザー
answer CHANGED
@@ -59,9 +59,9 @@
59
59
  Dim operation As String
60
60
  Dim parameter As String
61
61
 
62
- operation = "open" & vbNullChar & vbNullChar
62
+ operation = "open"
63
- exe = "powershell.exe" & vbNullChar & vbNullChar
63
+ exe = "powershell.exe"
64
- parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force" & vbNullChar & vbNullChar
64
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path " & src & " -DestinationPath " & dest & " -Force"
65
65
  Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
66
66
  End Sub
67
67
  ```

4

サンプル追記

2021/09/15 00:44

投稿

退会済みユーザー
answer CHANGED
@@ -11,4 +11,57 @@
11
11
  こんなもんでしょうか。
12
12
 
13
13
  [これで解消!「KB2704299」でCompress-Archiveの文字化け対処](https://cheshire-wara.com/powershell/ps-column/compress-archive-resolved/)
14
- こんな記事がありましたが、上記環境に該当してないですか?
14
+ こんな記事がありましたが、上記環境に該当してないですか?
15
+
16
+ こちらで動作確認したコードを載せておきます。日本語のフォルダ名やファイル名でも、特に問題ありませんでした。
17
+ ```vba
18
+ Private Declare PtrSafe Function ShellExecuteW Lib "shell32.dll" ( _
19
+ ByVal hwnd As LongPtr, _
20
+ ByVal lpOperation As LongPtr, _
21
+ ByVal lpFile As LongPtr, _
22
+ ByVal lpParameters As LongPtr, _
23
+ ByVal lpDirectory As LongPtr, _
24
+ ByVal nShowCmd As Long _
25
+ ) As LongPtr
26
+
27
+ Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As Long)
28
+
29
+ Const SW_HIDE = 0 'ウィンドウを表示しない
30
+ Const SW_SHOW = 5 'ウィンドウを現在の位置とサイズで表示
31
+
32
+ Private Sub CommandButton1_Click()
33
+ Dim src As String
34
+ src = "c:\test\あいうえお"
35
+
36
+ Call CreateZipWsh(src, "c:\test\test_wsh.zip")
37
+ Call CreateZipShellExecuteW(src, "c:\test\test_shellw.zip")
38
+ End Sub
39
+
40
+ 'WScript.Shellで実行
41
+ Private Sub CreateZipWsh(ByVal src As String, ByVal dest As String)
42
+ Dim parameter As String
43
+ Dim oWsh As Object
44
+ Dim oExec As Object
45
+
46
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force"
47
+ Set oWsh = CreateObject("WScript.Shell")
48
+ Set oExec = oWsh.Exec("powershell.exe" & " " & parameter)
49
+ Do While oExec.Status = 0
50
+ Sleep 1
51
+ Loop
52
+ Set oExec = Nothing
53
+ Set oWsh = Nothing
54
+ End Sub
55
+
56
+ 'ShellExecuteWで実行
57
+ Private Sub CreateZipShellExecuteW(ByVal src As String, ByVal dest As String)
58
+ Dim exe As String
59
+ Dim operation As String
60
+ Dim parameter As String
61
+
62
+ operation = "open" & vbNullChar & vbNullChar
63
+ exe = "powershell.exe" & vbNullChar & vbNullChar
64
+ parameter = "-NoLogo -ExecutionPolicy RemoteSigned -Command Compress-Archive -Path '" & src & "' -DestinationPath '" & dest & "' -Force" & vbNullChar & vbNullChar
65
+ Call ShellExecuteW(0, StrPtr(operation), StrPtr(exe), StrPtr(parameter), 0, SW_HIDE)
66
+ End Sub
67
+ ```

3

追記

2021/09/14 08:43

投稿

退会済みユーザー
answer CHANGED
@@ -2,9 +2,13 @@
2
2
  割と根が深い問題なので、ヘッダをアテにせず、独自に文字コード判定を行っている解凍ソフトもあります。
3
3
 
4
4
  考えられる解決策としては、
5
- - 何とかしてPowerShellにファイル名を渡す(PowerShellのスクリプトファイルを出力して実行するとか?)
5
+ - ~~何とかしてPowerShellにファイル名を渡す(PowerShellのスクリプトファイルを出力して実行するとか?)~~
6
+ エンコードせずにpowershellコマンドに渡して、文字化けしない事を確認しました。(Windows10)
6
7
  - Compress-Archiveの代替手段を探す
7
8
  - 出力されたZIPを何らかの手段で修復・変換する
8
9
  (ヘッダがおかしいか、ファイル名がおかしいかを特定する必要あり)
9
10
 
10
- こんなもんでしょうか。
11
+ こんなもんでしょうか。
12
+
13
+ [これで解消!「KB2704299」でCompress-Archiveの文字化け対処](https://cheshire-wara.com/powershell/ps-column/compress-archive-resolved/)
14
+ こんな記事がありましたが、上記環境に該当してないですか?

2

追記

2021/09/14 05:23

投稿

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

1

修正

2021/09/14 02:59

投稿

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