-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathPlexusIoZipFileResourceCollection.java
182 lines (153 loc) · 5.67 KB
/
PlexusIoZipFileResourceCollection.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
package org.codehaus.plexus.archiver.zip;
/*
* Copyright 2007 The Codehaus Foundation.
*
* 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.
*/
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier;
import org.codehaus.plexus.components.io.resources.AbstractPlexusIoArchiveResourceCollection;
import org.codehaus.plexus.components.io.resources.EncodingSupported;
import org.codehaus.plexus.components.io.resources.PlexusIoResource;
import org.codehaus.plexus.components.io.resources.PlexusIoURLResource;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Iterator;
public class PlexusIoZipFileResourceCollection
extends AbstractPlexusIoArchiveResourceCollection
implements EncodingSupported
{
/**
* The zip file resource collections role hint.
*/
public static final String ROLE_HINT = "zipFile";
/**
* The zip file resource collections role hint for jar files.
*/
public static final String JAR_ROLE_HINT = "jarFile";
private Charset charset = Charset.forName("UTF-8");
public PlexusIoZipFileResourceCollection()
{
}
protected Iterator<PlexusIoResource> getEntries()
throws IOException
{
final File f = getFile();
if ( f == null )
{
throw new IOException( "The zip file has not been set." );
}
final URLClassLoader urlClassLoader = new URLClassLoader( new URL[]{ f.toURI().toURL() }, null )
{
public URL getResource( String name )
{
return findResource( name );
}
};
final URL url = new URL( "jar:" + f.toURI().toURL() + "!/" );
final ZipFile zipFile = new ZipFile( f, charset != null ? charset.name() : "UTF8");
final Enumeration<ZipArchiveEntry> en = zipFile.getEntriesInPhysicalOrder();
return new ZipFileResourceIterator( en, url, zipFile, urlClassLoader );
}
private static class ZipFileResourceIterator
implements Iterator<PlexusIoResource>, Closeable
{
private class ZipFileResource
extends PlexusIoURLResource
{
private ZipFileResource( ZipArchiveEntry entry )
{
super( entry.getName(), entry.getTime() == -1 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : entry.getTime(),
entry.isDirectory() ? PlexusIoResource.UNKNOWN_RESOURCE_SIZE : entry.getSize(),
!entry.isDirectory(), entry.isDirectory(), true );
}
public URL getURL()
throws IOException
{
String spec = getName();
if ( spec.startsWith( "/" ) )
{
// Code path for PLXCOMP-170. Note that urlClassloader does not seem to produce correct
// urls for this. Which again means files loaded via this path cannot have file names
// requiring url encoding
spec = "./" + spec;
return new URL( url, spec );
}
return urlClassLoader.getResource( spec );
}
}
private class ZipFileSymlinkResource
extends ZipFileResource
implements SymlinkDestinationSupplier
{
private final ZipArchiveEntry entry;
private ZipFileSymlinkResource( ZipArchiveEntry entry )
{
super( entry );
this.entry = entry;
}
@Override
public String getSymlinkDestination()
throws IOException
{
return zipFile.getUnixSymlink( entry );
}
@Override
public boolean isSymbolicLink()
{
return true;
}
}
private final Enumeration<ZipArchiveEntry> en;
private final URL url;
private final ZipFile zipFile;
private final URLClassLoader urlClassLoader;
public ZipFileResourceIterator( Enumeration<ZipArchiveEntry> en, URL url, ZipFile zipFile, URLClassLoader urlClassLoader )
{
this.en = en;
this.url = url;
this.zipFile = zipFile;
this.urlClassLoader = urlClassLoader;
}
public boolean hasNext()
{
return en.hasMoreElements();
}
public PlexusIoResource next()
{
final ZipArchiveEntry entry = en.nextElement();
return entry.isUnixSymlink()
? new ZipFileSymlinkResource( entry )
: new ZipFileResource( entry );
}
public void remove()
{
throw new UnsupportedOperationException( "Removing isn't implemented." );
}
public void close()
throws IOException
{
zipFile.close();
}
}
public void setEncoding( Charset charset )
{
this.charset = charset;
}
}