1 /*
2 * Copyright (c) 2002-2017, Mairie de Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.plugins.form.modules.processornotifysender.utils;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37 import fr.paris.lutece.util.string.StringUtil;
38
39 import org.apache.commons.io.IOUtils;
40 import org.apache.commons.lang.StringUtils;
41
42 import java.io.BufferedOutputStream;
43 import java.io.File;
44 import java.io.FileInputStream;
45 import java.io.FileNotFoundException;
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48
49 import java.util.zip.ZipEntry;
50 import java.util.zip.ZipOutputStream;
51
52 /**
53 *
54 * ZipUtils
55 *
56 */
57 public final class ZipUtils
58 {
59 private static final String MESSAGE_DELETE_ERROR = "Error deleting file or folder : ";
60
61 /**
62 * Private constructor
63 */
64 private ZipUtils( )
65 {
66 }
67
68 /**
69 * This Method zip a folder and all files contained in it.
70 *
71 * @param strFolderToZip
72 * the folder to zip
73 * @param strZipDestination
74 * the place for the created zip
75 * @param strZipName
76 * the zip name
77 */
78 public static void zipFolder( String strFolderToZip, String strZipDestination, String strZipName )
79 {
80 FileUtils.createFolder( strZipDestination );
81
82 // Delete zip if it exists
83 File fileToDelete = new File( strZipDestination + strZipName );
84
85 if ( fileToDelete.exists( ) )
86 {
87 if ( !fileToDelete.delete( ) )
88 {
89 AppLogService.error( MESSAGE_DELETE_ERROR + strZipDestination + strZipName );
90 }
91 }
92
93 File folderToZip = new File( strFolderToZip );
94 ZipOutputStream zos = null;
95
96 try
97 {
98 BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( strZipDestination + strZipName ) );
99 zos = new ZipOutputStream( bos );
100
101 zipFolder( folderToZip, zos, StringUtils.EMPTY );
102 }
103 catch( FileNotFoundException e )
104 {
105 AppLogService.error( e );
106 }
107 catch( IOException e )
108 {
109 AppLogService.error( e );
110 }
111 finally
112 {
113 IOUtils.closeQuietly( zos );
114 }
115 }
116
117 /**
118 * This Method zip a folder and all files contained in it.
119 *
120 * @param dir
121 * folder to zip
122 * @param zos
123 * zip object
124 * @param path
125 * Current path in the zip
126 * @exception IOException
127 * exception if there is an error
128 */
129 private static void zipFolder( File dir, ZipOutputStream zos, String path ) throws IOException
130 {
131 if ( ( dir != null ) && dir.isDirectory( ) )
132 {
133 zipFileInFolder( dir, zos, path );
134 }
135 }
136
137 /**
138 * Zip a given directory ( Recursive function )
139 *
140 * @param dir
141 * Current directory to zip
142 * @param zos
143 * Zip object
144 * @param path
145 * Current path in the zip object
146 * @exception IOException
147 * exception if there is an error
148 */
149 private static void zipFileInFolder( File dir, ZipOutputStream zos, String path ) throws IOException
150 {
151 if ( ( dir != null ) && dir.isDirectory( ) )
152 {
153 File [ ] entries = dir.listFiles( );
154 int sz = entries.length;
155
156 for ( int j = 0; j < sz; j++ )
157 {
158 if ( entries [j].isDirectory( ) )
159 {
160 // FOLDER
161
162 // Add the new folder in the zip
163 ZipEntry ze = new ZipEntry( path + entries [j].getName( ) + File.separator );
164 zos.putNextEntry( ze );
165
166 // Call method zipFolder
167 File newDir = new File( dir.getAbsolutePath( ) + File.separator + entries [j].getName( ) );
168 zipFolder( newDir, zos, path + entries [j].getName( ) + File.separator );
169 }
170 else
171 {
172 // FILE
173
174 // Read the current file
175 FileInputStream bis = null;
176
177 try
178 {
179 bis = new FileInputStream( entries [j].getAbsolutePath( ) );
180
181 // Create new entry for the zip
182 ZipEntry ze = new ZipEntry( path + StringUtil.replaceAccent( entries [j].getName( ) ) );
183 byte [ ] tab = IOUtils.toByteArray( bis );
184
185 // Add the new entry to the zip
186 zos.putNextEntry( ze );
187 zos.write( tab );
188 zos.closeEntry( );
189 }
190 catch( FileNotFoundException e )
191 {
192 AppLogService.error( e );
193 }
194 finally
195 {
196 IOUtils.closeQuietly( bis );
197 }
198 }
199 }
200 }
201 }
202 }