回答編集履歴
2
zipOutStream.close(); の追加
answer
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
参考にさせていただいた内容をもとに、以下のように実装しました。
|
2
2
|
※パラメータの inputStream は MultipartFile から作成したもので、zip ファイルです。
|
3
3
|
|
4
|
+
バイト配列の取得位置が try-with-resources ではまだ ZipOutputStream を close していない場所だったので、ret = baos.toByteArray(); の前に、明示的に zipOutStream.close(); を入れました。
|
5
|
+
|
6
|
+
これで、7-zipの場合の解凍時のペイロードのメッセージも無くなり、また、Windowsエクスプローラでのダブルクリックでも解凍できるようになりました。
|
7
|
+
|
4
8
|
```
|
5
9
|
public byte[] sampleZip(InputStream inputStream) {
|
6
10
|
|
7
|
-
byte[] ret = new byte[
|
11
|
+
byte[] ret = new byte[1048476];
|
8
12
|
|
9
13
|
ZipEntry zipEntry;
|
10
14
|
ZipEntry zipOutputEntry;
|
@@ -53,6 +57,7 @@
|
|
53
57
|
zipInStream.closeEntry();
|
54
58
|
zipOutStream.closeEntry();
|
55
59
|
}
|
60
|
+
zipOutStream.close(); //追加 2020.03.05
|
56
61
|
ret = baos.toByteArray();
|
57
62
|
} catch (Exception e) {
|
58
63
|
ret = null;
|
1
最終的な実装は try-with-resources 文の記述に変更しました。
answer
CHANGED
@@ -2,19 +2,17 @@
|
|
2
2
|
※パラメータの inputStream は MultipartFile から作成したもので、zip ファイルです。
|
3
3
|
|
4
4
|
```
|
5
|
-
|
5
|
+
public byte[] sampleZip(InputStream inputStream) {
|
6
6
|
|
7
7
|
byte[] ret = new byte[1024];
|
8
8
|
|
9
|
-
ZipInputStream zipInStream = new ZipInputStream(new BufferedInputStream(inputStream), Charset.forName("SJIS"));
|
10
|
-
|
11
|
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
12
|
-
ZipOutputStream zipOutStream = new ZipOutputStream(baos);
|
13
|
-
|
14
9
|
ZipEntry zipEntry;
|
15
10
|
ZipEntry zipOutputEntry;
|
16
11
|
|
12
|
+
try (ZipInputStream zipInStream = new ZipInputStream(new BufferedInputStream(inputStream),
|
17
|
-
|
13
|
+
Charset.forName("SJIS"));
|
14
|
+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
15
|
+
ZipOutputStream zipOutStream = new ZipOutputStream(baos);) {
|
18
16
|
while ((zipEntry = zipInStream.getNextEntry()) != null) {
|
19
17
|
if (zipEntry.isDirectory()) {
|
20
18
|
continue;
|
@@ -41,7 +39,7 @@
|
|
41
39
|
}
|
42
40
|
|
43
41
|
XSSFWorkbook workbook = new XSSFWorkbook();
|
44
|
-
|
42
|
+
|
45
43
|
workbook = addSheet(new ByteArrayInputStream(fileBin)); // addSheet:独自の関数
|
46
44
|
fileBin = getBytes(workbook);
|
47
45
|
if (workbook != null) {
|
@@ -55,11 +53,8 @@
|
|
55
53
|
zipInStream.closeEntry();
|
56
54
|
zipOutStream.closeEntry();
|
57
55
|
}
|
58
|
-
zipInStream.close();
|
59
|
-
zipOutStream.close();
|
60
56
|
ret = baos.toByteArray();
|
61
|
-
} catch (
|
57
|
+
} catch (Exception e) {
|
62
|
-
// TODO 自動生成された catch ブロック
|
63
58
|
ret = null;
|
64
59
|
}
|
65
60
|
|