压缩类ZipEncryptOutputStream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import java.io.IOException;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import static com.zk.zip.ZipExpand.*;

/**
* Output stream that can be used to password-protect zip files.
*
* <h3>Example usage:</h3>
* <p>Creating a password-protected zip file:</p>
* <pre>
* ZipEncryptOutputStream zeos = new ZipEncryptOutputStream(new FileOutputStream(fileName), password);
* ZipOutputStream zos = new ZipOuputStream(zdis);
* ... create zip file using the standard JDK ZipOutputStream in zos variable ...
* </pre>
* <p>Converting a plain zip file to a password-protected zip file:</p>
* <pre>
* FileInputStream src = new FileInputStream(srcFile);
* ZipEncryptOutputStream dest = new ZipEncryptOutputStream(new FileOutputStream(destFile), password);
*
* // should wrap with try-catch-finally, do the close in finally
* int b;
* while ((b = src.read()) > -1) {
* dest.write(b);
* }
*
* src.close();
* dest.close();
* </pre>
*/
public class ZipEncryptOutputStream extends OutputStream {
private final OutputStream delegate;
private final int keys[] = new int[3];
private final int pwdKeys[] = new int[3];

private int copyBytes;
private int skipBytes;
private State state = State.NEW_SECTION;
private State futureState;
private Section section;
private byte[] decryptHeader;
private final ArrayList<int[][]> crcAndSize = new ArrayList<int[][]>();
private final ArrayList<Integer> localHeaderOffset = new ArrayList<Integer>();
private ArrayList<int[]> fileData;
private int[][] condition;
private int fileIndex;
private int[] buffer;
private int bufOffset;
private int fileSize;
private int bytesWritten;
private int centralRepoOffset;

private static final int ROW_SIZE = 65536;

/**
* Convenience constructor taking password as a string.
*
* @param delegate Output stream to write the password-protected zip to.
* @param password Password to use for protecting the zip.
*/
public ZipEncryptOutputStream(OutputStream delegate, String password) {
this(delegate, password.toCharArray());
}

/**
* Safer version of the constructor. Takes password as a char array that can
* be nulled right after calling this constructor instead of a string that may
* stay visible on the heap for the duration of application run time.
*
* @param delegate Output stream to write the password-protected zip to.
* @param password Password to use for protecting the zip.
*/
public ZipEncryptOutputStream(OutputStream delegate, char[] password) {
this.delegate = delegate;
pwdKeys[0] = 305419896;
pwdKeys[1] = 591751049;
pwdKeys[2] = 878082192;
for (int i = 0; i < password.length; i++) {
ZipExpand.updateKeys((byte) (password[i] & 0xff), pwdKeys);
}
}

private static enum State {NEW_SECTION, SECTION_HEADER, FLAGS, REPO_OFFSET, CRC, FILE_HEADER_OFFSET,
COMPRESSED_SIZE_READ, HEADER, DATA, FILE_BUFFERED, BUFFER, BUFFER_COPY, BUFFER_UNTIL, TAIL}
private static enum Section {LFH, CFH, ECD}

@Override
public void write(int b) throws IOException {
if (skipBytes > 0) {
skipBytes--;
return;
}
if (copyBytes == 0) {
switch (state) {
case NEW_SECTION:
if (b != 0x50) {
throw new IllegalStateException("Unexpected value read at offset " + bytesWritten + ": " + b + " (expected: " + 0x50 + ")");
}
buffer(new int[4], State.SECTION_HEADER, 0x50);
return;
case SECTION_HEADER:
identifySectionHeader();
break;
case FLAGS:
copyBytes = 7;
state = State.CRC;
if (section == Section.LFH) {
if ((b & 1) == 1) {
throw new IllegalStateException("ZIP already password protected.");
}
if ((b & 64) == 64) {
throw new IllegalStateException("Strong encryption used.");
}
if ((b & 8) == 8) {
bufferUntil(State.FILE_BUFFERED, CFH_SIGNATURE, LFH_SIGNATURE);
}
}
b = b & 0xf7 | 1;
break;
case CRC:
if (section == Section.CFH) {
int[][] cns = crcAndSize.get(fileIndex);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 4; i++) {
writeToDelegate(cns[j][i]);
}
}
skipBytes = 11;
copyBytes = 14;
state = State.FILE_HEADER_OFFSET;
} else {
int[] cns = new int[16];
buffer(cns, State.COMPRESSED_SIZE_READ, b);
}
return;
case FILE_HEADER_OFFSET:
writeAsBytes(localHeaderOffset.get(fileIndex));
fileIndex++;
skipBytes = 3;
copyBytesUntil(State.SECTION_HEADER, CFH_SIGNATURE, ECD_SIGNATURE);
return;
case COMPRESSED_SIZE_READ:
int[][] cns = new int[][] {
{buffer[0], buffer[1], buffer[2], buffer[3]},
{buffer[4], buffer[5], buffer[6], buffer[7]},
{buffer[8], buffer[9], buffer[10], buffer[11]}
};
adjustSize(cns[1]);
crcAndSize.add(cns);
for (int j = 0; j < 3; j++) {
for (int i = 0; i < 4; i++) {
writeToDelegate(cns[j][i]);
}
}
copyBytes = buffer[12] + buffer[14] + (buffer[13] + buffer[15]) * 256 - 1;
state = State.HEADER;
if (copyBytes < 0) {
throw new IllegalStateException("No file name stored in the zip file.");
}
break;
case HEADER:
writeDecryptHeader();
fileSize = decode(crcAndSize.get(crcAndSize.size() - 1)[1]);
state = State.DATA;
// intentionally no break
case DATA:
b = encrypt(b);
fileSize--;
if (fileSize == 0) {
state = State.NEW_SECTION;
}
break;
case BUFFER:
buffer[bufOffset] = b;
bufOffset++;
if (bufOffset == buffer.length) {
state = futureState;
}
return;
case BUFFER_COPY:
buffer[bufOffset] = b;
if (checkCondition()) {
bufOffset = 0;
state = futureState;
}
break;
case BUFFER_UNTIL:
int col = fileSize % ROW_SIZE;
if (col == 0) {
fileData.add(new int[ROW_SIZE]);
}
int[] row = fileData.get(fileData.size() - 1);
row[col] = b;
buffer[bufOffset] = b;
fileSize++;
if (checkCondition()) {
fileSize -= buffer.length;
state = futureState;
}
return;
case FILE_BUFFERED:
row = fileData.get(0);
int r = 0;
int pointer = 16 + row[12] + row[14] + (row[13] + row[15]) * 256;
cns = new int[3][4];
readFromFileBuffer(fileSize - 12, cns[0]);
readFromFileBuffer(fileSize - 8, cns[1]);
readFromFileBuffer(fileSize - 4, cns[2]);
fileSize = decode(cns[1]);
adjustSize(cns[1]);
crcAndSize.add(cns);
for (int i = 0; i < 4; i++) {
row[i] = cns[0][i];
row[i + 4] = cns[1][i];
row[i + 8] = cns[2][i];
}
for (int i = 0; i < pointer; i++) {
writeToDelegate(row[i]);
}
writeDecryptHeader();
for (int i = 0; i < fileSize; i++) {
writeToDelegate(encrypt(row[pointer]));
pointer++;
if (pointer == ROW_SIZE) {
pointer = 0;
r++;
row = fileData.get(r);
}
}
fileData = null;
identifySectionHeader();
break;
case REPO_OFFSET:
writeAsBytes(centralRepoOffset);
skipBytes = 3;
state = State.TAIL;
return;
case TAIL:
break;
}
} else {
copyBytes--;
}
writeToDelegate(b);
}

private void writeToDelegate(int b) throws IOException {
delegate.write(b);
bytesWritten++;
}

private static void adjustSize(int[] values) {
int inc = DECRYPT_HEADER_SIZE;
for (int i = 0; i < 4; i++) {
values[i] = values[i] + inc;
inc = values[i] >> 8;
values[i] &= 0xff;
}
}

private static int decode(int[] value) {
return value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] << 24);
}

private void writeAsBytes(int value) throws IOException {
for (int i = 0; i < 4; i++) {
writeToDelegate(value & 0xff);
value >>= 8;
}
}

private void identifySectionHeader() throws IllegalStateException, IOException {
if (Arrays.equals(buffer, LFH_SIGNATURE)) {
section = Section.LFH;
copyBytes = 1;
state = State.FLAGS;
localHeaderOffset.add(bytesWritten);
} else if (Arrays.equals(buffer, CFH_SIGNATURE)) {
section = Section.CFH;
copyBytes = 3;
state = State.FLAGS;
if (centralRepoOffset == 0) {
centralRepoOffset = bytesWritten;
}
} else if (Arrays.equals(buffer, ECD_SIGNATURE)) {
section = Section.ECD;
copyBytes = 11;
state = State.REPO_OFFSET;
} else {
throw new IllegalStateException("Unknown header: " + Arrays.asList(buffer).toString());
}
flushBuffer();
}

private void readFromFileBuffer(int offset, int[] target) {
int r = offset / ROW_SIZE;
int c = offset % ROW_SIZE;
int[] row = fileData.get(r);
for (int i = 0; i < target.length; i++) {
target[i] = row[c];
c++;
if (c == ROW_SIZE) {
c = 0;
r++;
row = fileData.get(r);
}
}
}

@Override
public void close() throws IOException {
super.close();
delegate.close();
}

private void initKeys() {
System.arraycopy(pwdKeys, 0, keys, 0, keys.length);
}

private void updateKeys(byte charAt) {
ZipExpand.updateKeys(charAt, keys);
}

private byte encryptByte() {
int temp = keys[2] | 2;
return (byte) ((temp * (temp ^ 1)) >>> 8);
}

private int encrypt(int b) {
int newB = (b ^ encryptByte()) & 0xff;
updateKeys((byte) b);
return newB;
}

private void writeDecryptHeader() throws IOException {
initKeys();
int[] crc = crcAndSize.get(crcAndSize.size() - 1)[0];
SecureRandom random = new SecureRandom();
decryptHeader = new byte[DECRYPT_HEADER_SIZE];
random.nextBytes(decryptHeader);
decryptHeader[DECRYPT_HEADER_SIZE - 2] = (byte) crc[2];
decryptHeader[DECRYPT_HEADER_SIZE - 1] = (byte) crc[3];
for (int i = 0; i < DECRYPT_HEADER_SIZE; i++) {
writeToDelegate(encrypt(decryptHeader[i]));
}
}

private void buffer(int[] values, State state, int... knownValues) {
System.arraycopy(knownValues, 0, values, 0, knownValues.length);
buffer = values;
bufOffset = knownValues.length;
this.state = State.BUFFER;
futureState = state;
}

private void flushBuffer() throws IOException {
for (int i = 0; i < bufOffset; i++) {
writeToDelegate(buffer[i]);
}
}

private void copyBytesUntil(State state, int[]... condition) {
futureState = state;
this.condition = condition;
bufOffset = 0;
buffer = new int[condition[0].length];
this.state = State.BUFFER_COPY;
}

private void bufferUntil(State state, int[]... condition) {
copyBytesUntil(state, condition);
fileData = new ArrayList<int[]>();
fileSize = 0;
this.state = State.BUFFER_UNTIL;
}

private boolean checkCondition() {
boolean equals = true;
for (int i = 0; i < condition.length; i++) {
equals = true;
for (int j = 0; j <= bufOffset; j++) {
if (condition[i][j] != buffer[j]) {
equals = false;
break;
}
}
if (equals) {
bufOffset++;
break;
}
}
if (!equals) {
bufOffset = 0;
}
return equals && (buffer.length == bufOffset);
}
}

##解压缩类ZipDecryptInputStream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

import java.io.IOException;
import java.io.InputStream;
import static com.zk.zip.ZipExpand.DD_SIGNATURE;
import static com.zk.zip.ZipExpand.DECRYPT_HEADER_SIZE;
import static com.zk.zip.ZipExpand.LFH_SIGNATURE;
import com.zk.zip.ZipExpand.Section;
import com.zk.zip.ZipExpand.State;

/**
* Input stream converting a password-protected zip to an unprotected zip.
*
* <h3>Example usage:</h3>
* <p>Reading a password-protected zip from file:</p>
* <pre>
* ZipDecryptInputStream zdis = new ZipDecryptInputStream(new FileInputStream(fileName), password);
* ZipInputStream zis = new ZipInputStream(zdis);
* ... read the zip file from zis - the standard JDK ZipInputStream ...
* </pre>
* <p>Converting a password-protected zip file to an unprotected zip file:</p>
* <pre>
* ZipDecryptInputStream src = new ZipDecryptInputStream(new FileInputStream(srcFile), password);
* FileOutputStream dest = new FileOutputStream(destFile);
*
* // should wrap with try-catch-finally, do the close in finally
* int b;
* while ((b = src.read()) > -1) {
* dest.write(b);
* }
*
* src.close();
* dest.close();
* </pre>
*/
public class ZipDecryptInputStream extends InputStream {
private final InputStream delegate;
private final int keys[] = new int[3];
private final int pwdKeys[] = new int[3];

private State state = State.SIGNATURE;
private Section section;
private int skipBytes;
private int compressedSize;
private int crc;

/**
* Creates a new instance of the stream.
*
* @param stream Input stream serving the password-protected zip file to be decrypted.
* @param password Password to be used to decrypt the password-protected zip file.
*/
public ZipDecryptInputStream(InputStream stream, String password) {
this(stream, password.toCharArray());
}

/**
* Safer constructor. Takes password as a char array that can be nulled right after
* calling this constructor instead of a string that may be visible on the heap for
* the duration of application run time.
*
* @param stream Input stream serving the password-protected zip file.
* @param password Password to use for decrypting the zip file.
*/
public ZipDecryptInputStream(InputStream stream, char[] password) {
this.delegate = stream;
pwdKeys[0] = 305419896;
pwdKeys[1] = 591751049;
pwdKeys[2] = 878082192;
for (int i = 0; i < password.length; i++) {
ZipExpand.updateKeys((byte) (password[i] & 0xff), pwdKeys);
}
}

@SuppressWarnings("incomplete-switch")
@Override
public int read() throws IOException {
int result = delegateRead();
if (skipBytes == 0) {
switch (state) {
case SIGNATURE:
if (!peekAheadEquals(LFH_SIGNATURE)) {
state = State.TAIL;
} else {
section = Section.FILE_HEADER;
skipBytes = 5;
state = State.FLAGS;
}
break;
case FLAGS:
if ((result & 1) == 0) {
throw new IllegalStateException("ZIP not password protected.");
}
if ((result & 64) == 64) {
throw new IllegalStateException("Strong encryption used.");
}
if ((result & 8) == 8) {
compressedSize = -1;
state = State.FN_LENGTH;
skipBytes = 19;
} else {
state = State.CRC;
skipBytes = 10;
}
result -= 1;
break;
case CRC:
crc = result;
state = State.COMPRESSED_SIZE;
break;
case COMPRESSED_SIZE:
int[] values = new int[4];
peekAhead(values);
compressedSize = 0;
int valueInc = DECRYPT_HEADER_SIZE;
for (int i = 0; i < 4; i++) {
compressedSize += values[i] << (8 * i);
values[i] -= valueInc;
if (values[i] < 0) {
valueInc = 1;
values[i] += 256;
} else {
valueInc = 0;
}
}
overrideBuffer(values);
result = values[0];
if (section == Section.DATA_DESCRIPTOR) {
state = State.SIGNATURE;
} else {
state = State.FN_LENGTH;
}
skipBytes = 7;
break;
case FN_LENGTH:
values = new int[4];
peekAhead(values);
skipBytes = 3 + values[0] + values[2] + (values[1] + values[3]) * 256;
state = State.HEADER;
break;
case HEADER:
section = Section.FILE_DATA;
initKeys();
byte lastValue = 0;
for (int i = 0; i < DECRYPT_HEADER_SIZE; i++) {
lastValue = (byte) (result ^ decryptByte());
updateKeys(lastValue);
result = delegateRead();
}
if ((lastValue & 0xff) != crc) {
// throw new IllegalStateException("Wrong password!");
}
compressedSize -= DECRYPT_HEADER_SIZE;
state = State.DATA;
// intentionally no break
case DATA:
if (compressedSize == -1 && peekAheadEquals(DD_SIGNATURE)) {
section = Section.DATA_DESCRIPTOR;
skipBytes = 5;
state = State.CRC;
} else {
result = (result ^ decryptByte()) & 0xff;
updateKeys((byte) result);
compressedSize--;
if (compressedSize == 0) {
state = State.SIGNATURE;
}
}
break;
case TAIL:
// do nothing
}
} else {
skipBytes--;
}
return result;
}

private static final int BUF_SIZE = 8;
private int bufOffset = BUF_SIZE;
private final int[] buf = new int[BUF_SIZE];

private int delegateRead() throws IOException {
bufOffset++;
if (bufOffset >= BUF_SIZE) {
fetchData(0);
bufOffset = 0;
}
return buf[bufOffset];
}

private boolean peekAheadEquals(int[] values) throws IOException {
prepareBuffer(values);
for (int i = 0; i < values.length; i++) {
if (buf[bufOffset + i] != values[i]) {
return false;
}
}
return true;
}

private void prepareBuffer(int[] values) throws IOException {
if (values.length > (BUF_SIZE - bufOffset)) {
for (int i = bufOffset; i < BUF_SIZE; i++) {
buf[i - bufOffset] = buf[i];
}
fetchData(BUF_SIZE - bufOffset);
bufOffset = 0;
}
}

private void peekAhead(int[] values) throws IOException {
prepareBuffer(values);
System.arraycopy(buf, bufOffset, values, 0, values.length);
}

private void overrideBuffer(int[] values) throws IOException {
prepareBuffer(values);
System.arraycopy(values, 0, buf, bufOffset, values.length);
}

private void fetchData(int offset) throws IOException {
for (int i = offset; i < BUF_SIZE; i++) {
buf[i] = delegate.read();
if (buf[i] == -1) {
break;
}
}
}

@Override
public void close() throws IOException {
delegate.close();
super.close();
}

private void initKeys() {
System.arraycopy(pwdKeys, 0, keys, 0, keys.length);
}

private void updateKeys(byte charAt) {
ZipExpand.updateKeys(charAt, keys);
}

private byte decryptByte() {
int temp = keys[2] | 2;
return (byte) ((temp * (temp ^ 1)) >>> 8);
}
}

##解密工具类ZipExpand.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

class ZipExpand {
static final int[] CRC_TABLE = new int[256];
// compute the table
// (could also have it pre-computed - see http://snippets.dzone.com/tag/crc32)
static {
for (int i = 0; i < 256; i++) {
int r = i;
for (int j = 0; j < 8; j++) {
if ((r & 1) == 1) {
r = (r >>> 1) ^ 0xedb88320;
} else {
r >>>= 1;
}
}
CRC_TABLE[i] = r;
}
}

static final int DECRYPT_HEADER_SIZE = 12;
static final int[] CFH_SIGNATURE = {0x50, 0x4b, 0x01, 0x02};
static final int[] LFH_SIGNATURE = {0x50, 0x4b, 0x03, 0x04};
static final int[] ECD_SIGNATURE = {0x50, 0x4b, 0x05, 0x06};
static final int[] DD_SIGNATURE = {0x50, 0x4b, 0x07, 0x08};

static void updateKeys(byte charAt, int[] keys) {
keys[0] = crc32(keys[0], charAt);
keys[1] += keys[0] & 0xff;
keys[1] = keys[1] * 134775813 + 1;
keys[2] = crc32(keys[2], (byte) (keys[1] >> 24));
}

static int crc32(int oldCrc, byte charAt) {
return ((oldCrc >>> 8) ^ CRC_TABLE[(oldCrc ^ charAt) & 0xff]);
}

static enum State {
SIGNATURE, FLAGS, COMPRESSED_SIZE, FN_LENGTH, EF_LENGTH, HEADER, DATA, TAIL, CRC
}

static enum Section {
FILE_HEADER, FILE_DATA, DATA_DESCRIPTOR
}
}

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

public static void main(String[] args) throws Exception {
try {
boolean a=true;
boolean b=a==false;

/**
* 压缩
*/
if(a){
//读取文件流
List<InputStream> list=new ArrayList<InputStream>();
list.add(new FileInputStream(new File("C:\\Users\\zk\\Desktop\\a.txt")));
list.add(new FileInputStream(new File("C:\\Users\\zk\\Desktop\\b.txt")));
list.add(new FileInputStream(new File("C:\\Users\\zk\\Desktop\\c.txt")));
//获取输出流
ByteArrayOutputStream array=writeZipOutputStream(list.toArray(new FileInputStream[0]),new String[]{"a.txt","b.txt","c.txt"}, "123456");
//写入文件
File file=new File("C:\\Users\\zk\\Desktop\\a.zip");
if (!file.exists()) {
file.createNewFile();
}
DataOutputStream to=new DataOutputStream(new FileOutputStream(file));
to.write(array.toByteArray());
to.close();
array.close();
}

/**
* 解压
*/
if(b){
//获取文件字节数组
File file=new File("C:\\Users\\zk\\Desktop\\Desktop.zip");
byte[] data =new byte[(int) file.length()];
FileInputStream fileInputStream=new FileInputStream(file);
fileInputStream.read(data);
//解压文件获取文本内容
StringBuffer sb=readZip(data,"123456");
fileInputStream.close();
System.out.println(sb);
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 压缩
* @param inputStreams 输入流数组
* @param fileName 文件名数组
* @param password 密码
* @return
* @throws IOException
*/
public static ByteArrayOutputStream writeZipOutputStream(InputStream [] inputStreams,String [] fileName,String password) throws IOException{
ZipOutputStream zos = null;
ByteArrayOutputStream baos=null;
try {
baos=new ByteArrayOutputStream();
zos = new ZipOutputStream(new ZipEncryptOutputStream(baos, password));
for (int i = 0; i < inputStreams.length; i++) {
ZipEntry ze = new ZipEntry(fileName[i]);
zos.putNextEntry(ze);
InputStream is = null;
try {
is = inputStreams[i];
int b;
while ((b = is.read()) != -1) {
zos.write(b);
}
} finally{
if(is!=null){
is.close();
}
}
}
} finally{
if(zos!=null){
zos.closeEntry();
zos.close();
}
}
return baos;
}


/**
* 解压缩
* @param data zip字节数组
* @param password zip密码
* @return
* @throws IOException
*/
public static StringBuffer readZip(byte[] data,String password) throws IOException {
StringBuffer sb = new StringBuffer();
ZipInputStream zin = null;
DataInputStream dis = null;
try {
zin = new ZipInputStream(new ZipDecryptInputStream(new BufferedInputStream(new ByteArrayInputStream(data)), password));
dis = new DataInputStream(zin);//用ZIP输入流构建DataInputStream
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
} else {
long size = ze.getSize();
System.out.println(ze.getName());
if (size > 0) {
BufferedReader br = null;
try {
byte[] content = new byte[(int) ze.getSize()];
dis.readFully(content);
br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content)));
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}

}finally{
if(br!=null)
br.close();
}
}

}
}
}finally{
if(zin!=null){
zin.closeEntry();
zin.close();
}
if(dis!=null)
dis.close();
}
return sb;
}
}