View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   /*
4    * Copyright (C) 2011-2016, Neticoa SAS France - Tous droits réservés.
5    * Author(s) : Franck Bonin, Neticoa SAS France
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   *
19   */
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.net.URL;
25  //import java.util.Date;
26  import java.util.ArrayList;
27  import java.util.Enumeration;
28  import java.util.List;
29  
30  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
31  import org.apache.commons.compress.archivers.zip.ZipFile;
32  import org.apache.commons.compress.utils.IOUtils;
33  import org.codehaus.plexus.archiver.AbstractArchiveContentLister;
34  import org.codehaus.plexus.archiver.ArchiveContentEntry;
35  //import org.codehaus.plexus.archiver.ArchiveFilterException;
36  import org.codehaus.plexus.archiver.ArchiverException;
37  import org.codehaus.plexus.components.io.resources.PlexusIoResource;
38  
39  /**
40   * @author Franck Bonin
41   * @version $Revision$ $Date$
42   */
43  public abstract class AbstractZipArchiveContentLister
44      extends AbstractArchiveContentLister
45  {
46      private static final String NATIVE_ENCODING = "native-encoding";
47  
48      private String encoding = "UTF8";
49  
50      public AbstractZipArchiveContentLister()
51      {
52      }
53  
54      public AbstractZipArchiveContentLister( final File sourceFile )
55      {
56          super( sourceFile );
57      }
58  
59      /**
60       * Sets the encoding to assume for file names and comments.
61       * <p/>
62       * <p>
63       * Set to <code>native-encoding</code> if you want your platform's native encoding, defaults to UTF8.
64       * </p>
65       */
66      public void setEncoding( String encoding )
67      {
68          if ( NATIVE_ENCODING.equals( encoding ) )
69          {
70              encoding = null;
71          }
72          this.encoding = encoding;
73      }
74  
75      private static class ZipEntryFileInfo
76          implements PlexusIoResource
77      {
78          private final ZipFile zipFile;
79  
80          private final ZipArchiveEntry zipEntry;
81  
82          ZipEntryFileInfo( final org.apache.commons.compress.archivers.zip.ZipFile zipFile,
83              final ZipArchiveEntry zipEntry )
84          {
85              this.zipFile = zipFile;
86              this.zipEntry = zipEntry;
87          }
88  
89          public String getName()
90          {
91              return zipEntry.getName();
92          }
93  
94          public boolean isDirectory()
95          {
96              return zipEntry.isDirectory();
97          }
98  
99          public boolean isFile()
100         {
101             return !zipEntry.isDirectory() && !zipEntry.isUnixSymlink();
102         }
103 
104         public boolean isSymbolicLink()
105         {
106             return zipEntry.isUnixSymlink();
107         }
108 
109         public InputStream getContents()
110             throws IOException
111         {
112             return zipFile.getInputStream( zipEntry );
113         }
114 
115         public long getLastModified()
116         {
117             final long l = zipEntry.getTime();
118             return l == 0 ? PlexusIoResource.UNKNOWN_MODIFICATION_DATE : l;
119         }
120 
121         public long getSize()
122         {
123             final long l = zipEntry.getSize();
124             return l == -1 ? PlexusIoResource.UNKNOWN_RESOURCE_SIZE : l;
125         }
126 
127         public URL getURL()
128             throws IOException
129         {
130             return null;
131         }
132 
133         public boolean isExisting()
134         {
135             return true;
136         }
137         
138         public ArchiveContentEntry asArchiveContentEntry()
139         {
140             if ( isSymbolicLink() )
141             {
142                 return ArchiveContentEntry.createSymlinkEntry( getName(), this, -1 );
143             }
144             else if ( isDirectory() )
145             {
146                 return ArchiveContentEntry.createDirectoryEntry( getName(), this, -1 );
147             }
148             else
149             {
150                 return ArchiveContentEntry.createFileEntry( getName(), this, -1 );
151             }
152         }
153     }
154     
155     protected List<ArchiveContentEntry> execute()
156         throws ArchiverException
157     {
158         ArrayList<ArchiveContentEntry> archiveContentList = new ArrayList<ArchiveContentEntry>();
159         
160         getLogger().debug( "listing: " + getSourceFile() );
161         org.apache.commons.compress.archivers.zip.ZipFile zf = null;
162         try
163         {
164             zf = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding );
165             final Enumeration e = zf.getEntries();
166             while ( e.hasMoreElements() )
167             {
168                 final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
169                 final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zf, ze );
170                 if ( isSelected( ze.getName(), fileInfo ) )
171                 {
172                     ArchiveContentEntry ae = fileInfo.asArchiveContentEntry();
173                     archiveContentList.add( ae );
174                 }
175             }
176             getLogger().debug( "listing complete" );
177         }
178         catch ( final IOException ioe )
179         {
180             throw new ArchiverException( "Error while listing " + getSourceFile().getAbsolutePath(), ioe );
181         }
182         finally
183         {
184             IOUtils.closeQuietly( zf );
185         }
186         return archiveContentList;
187     }
188 
189 /*
190     protected void execute()
191         throws ArchiverException
192     {
193         getLogger().debug( "Expanding: " + getSourceFile() + " into " + getDestDirectory() );
194         org.apache.commons.compress.archivers.zip.ZipFile zf = null;
195         try
196         {
197             zf = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding );
198             final Enumeration e = zf.getEntries();
199             while ( e.hasMoreElements() )
200             {
201                 final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
202                 final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zf, ze );
203                 if ( isSelected( ze.getName(), fileInfo ) )
204                 {
205                     InputStream in = zf.getInputStream( ze );
206                     extractFileIfIncluded(getSourceFile(), getDestDirectory(), in, ze.getName(),
207                             new Date(ze.getTime()), ze.isDirectory(), ze.getUnixMode() != 0 ? ze.getUnixMode() : null,
208                             resolveSymlink( zf, ze ) );
209                     IOUtil.close(in);
210                 }
211 
212             }
213 
214             getLogger().debug( "expand complete" );
215         }
216         catch ( final IOException ioe )
217         {
218             throw new ArchiverException( "Error while expanding " + getSourceFile().getAbsolutePath(), ioe );
219         }
220         finally
221         {
222             IOUtils.closeQuietly( zf);
223         }
224     }
225 
226     private String resolveSymlink( ZipFile zf, ZipArchiveEntry ze )
227         throws IOException
228     {
229         if  (ze.isUnixSymlink())
230             return zf.getUnixSymlink( ze );
231         else return null;
232     }
233 
234     private void extractFileIfIncluded( final File sourceFile, final File destDirectory, final InputStream inputStream,
235                                         final String name, final Date time, final boolean isDirectory,
236                                         final Integer mode, String symlinkDestination )
237         throws IOException, ArchiverException
238     {
239           extractFile( sourceFile, destDirectory, inputStream, name, time, isDirectory, mode, symlinkDestination );
240     }
241 
242     protected void execute( final String path, final File outputDirectory )
243         throws ArchiverException
244     {
245         org.apache.commons.compress.archivers.zip.ZipFile zipFile = null;
246 
247         try
248         {
249             zipFile = new org.apache.commons.compress.archivers.zip.ZipFile( getSourceFile(), encoding );
250 
251             final Enumeration e = zipFile.getEntries();
252 
253             while ( e.hasMoreElements() )
254             {
255                 final ZipArchiveEntry ze = (ZipArchiveEntry) e.nextElement();
256                 final ZipEntryFileInfo fileInfo = new ZipEntryFileInfo( zipFile, ze );
257                 if ( !isSelected( ze.getName(), fileInfo ) )
258                 {
259                     continue;
260                 }
261 
262                 if ( ze.getName().startsWith( path ) )
263                 {
264                     final InputStream inputStream = zipFile.getInputStream( ze );
265                     extractFileIfIncluded( getSourceFile(), outputDirectory, inputStream,
266                         ze.getName(), new Date( ze.getTime() ), ze.isDirectory(),
267                         ze.getUnixMode() != 0 ? ze.getUnixMode() : null, resolveSymlink( zipFile, ze ) );
268                     IOUtil.close(inputStream);
269                 }
270             }
271         }
272         catch ( final IOException ioe )
273         {
274             throw new ArchiverException( "Error while expanding " + getSourceFile().getAbsolutePath(), ioe );
275         }
276         finally
277         {
278             IOUtils.closeQuietly( zipFile);
279         }
280     }
281     */
282 }