-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
Zip.java
143 lines (130 loc) · 5.56 KB
/
Zip.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
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api.tasks.bundling;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.internal.file.archive.ZipCopyAction;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.internal.file.copy.DefaultZipCompressor;
import org.gradle.api.internal.file.copy.ZipCompressor;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.work.DisableCachingByDefault;
import javax.annotation.Nullable;
import java.nio.charset.Charset;
/**
* Assembles a ZIP archive.
*
* The default is to compress the contents of the zip.
*/
@DisableCachingByDefault(because = "Not worth caching")
public abstract class Zip extends AbstractArchiveTask {
public static final String ZIP_EXTENSION = "zip";
private ZipEntryCompression entryCompression = ZipEntryCompression.DEFLATED;
private boolean allowZip64;
private String metadataCharset;
public Zip() {
getArchiveExtension().set(ZIP_EXTENSION);
allowZip64 = false;
}
@Internal
protected ZipCompressor getCompressor() {
switch (entryCompression) {
case DEFLATED:
return new DefaultZipCompressor(allowZip64, ZipArchiveOutputStream.DEFLATED);
case STORED:
return new DefaultZipCompressor(allowZip64, ZipArchiveOutputStream.STORED);
default:
throw new IllegalArgumentException(String.format("Unknown Compression type %s", entryCompression));
}
}
@Override
protected CopyAction createCopyAction() {
DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class);
return new ZipCopyAction(getArchiveFile().get().getAsFile(), getCompressor(), documentationRegistry, metadataCharset, isPreserveFileTimestamps());
}
/**
* Returns the compression level of the entries of the archive. If set to {@link ZipEntryCompression#DEFLATED} (the default), each entry is
* compressed using the DEFLATE algorithm. If set to {@link ZipEntryCompression#STORED} the entries of the archive are left uncompressed.
*
* @return the compression level of the archive contents.
*/
@Input
public ZipEntryCompression getEntryCompression() {
return entryCompression;
}
/**
* Sets the compression level of the entries of the archive. If set to {@link ZipEntryCompression#DEFLATED} (the default), each entry is
* compressed using the DEFLATE algorithm. If set to {@link ZipEntryCompression#STORED} the entries of the archive are left uncompressed.
*
* @param entryCompression {@code STORED} or {@code DEFLATED}
*/
public void setEntryCompression(ZipEntryCompression entryCompression) {
this.entryCompression = entryCompression;
}
/**
* Enables building zips with more than 65535 files or bigger than 4GB.
*
* @see #isZip64()
*/
public void setZip64(boolean allowZip64) {
this.allowZip64 = allowZip64;
}
/**
* Whether the zip can contain more than 65535 files and/or support files greater than 4GB in size.
* <p>
* The standard zip format has hard limits on file size and count.
* The <a href="http://en.wikipedia.org/wiki/Zip_(file_format)#ZIP64">Zip64 format extension</a>
* practically removes these limits and is therefore required for building large zips.
* <p>
* However, not all Zip readers support the Zip64 extensions.
* Notably, the {@link java.util.zip.ZipInputStream} JDK class does not support Zip64 for versions earlier than Java 7.
* This means you should not enable this property if you are building JARs to be used with Java 6 and earlier runtimes.
*/
@Input
public boolean isZip64() {
return allowZip64;
}
/**
* The character set used to encode ZIP metadata like file names.
* Defaults to the platform's default character set.
*
* @return null if using the platform's default character set for ZIP metadata
* @since 2.14
*/
@Nullable @Optional @Input
public String getMetadataCharset() {
return this.metadataCharset;
}
/**
* The character set used to encode ZIP metadata like file names.
* Defaults to the platform's default character set.
*
* @param metadataCharset the character set used to encode ZIP metadata like file names
* @since 2.14
*/
public void setMetadataCharset(String metadataCharset) {
if (metadataCharset == null) {
throw new InvalidUserDataException("metadataCharset must not be null");
}
if (!Charset.isSupported(metadataCharset)) {
throw new InvalidUserDataException(String.format("Charset for metadataCharset '%s' is not supported by your JVM", metadataCharset));
}
this.metadataCharset = metadataCharset;
}
}