Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.lib / org.gvsig.installer.lib.impl / src / main / java / org / gvsig / installer / lib / impl / utils / Decompress.java @ 35189

History | View | Annotate | Download (13.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.lib.impl.utils;
29

    
30
import java.io.ByteArrayInputStream;
31
import java.io.ByteArrayOutputStream;
32
import java.io.File;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.Map;
39
import java.util.zip.ZipEntry;
40
import java.util.zip.ZipException;
41
import java.util.zip.ZipInputStream;
42

    
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

    
46
import org.gvsig.installer.lib.api.PackageInfo;
47
import org.gvsig.installer.lib.api.execution.InstallPackageServiceException;
48
import org.gvsig.installer.lib.impl.DefaultPackageInfo;
49
import org.gvsig.installer.lib.impl.info.InstallerInfoFileReader;
50
import org.gvsig.installer.lib.spi.InstallerInfoFileException;
51
import org.gvsig.installer.lib.spi.InstallerProviderLocator;
52
import org.gvsig.tools.ToolsLocator;
53
import org.gvsig.tools.task.SimpleTaskStatus;
54
import org.gvsig.tools.task.TaskStatusManager;
55

    
56
/**
57
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera Llodr&aacute;</a>
58
 */
59
public class Decompress {
60

    
61
    private static final int OPTION_DECOMPRESS = 1;
62
    private static final int OPTION_READ_INSTALLINFO = 2;
63
    private static final int OPTION_INSTALL = 3;
64

    
65
    private int option = 0;
66

    
67
    private int BUFFER = 2048;
68

    
69
    private static final Logger logger =
70
        LoggerFactory.getLogger(Decompress.class);
71

    
72
    private List<PackageInfo> readedIinstallerInfos = null;
73
    private File outputDirectory = null;
74
    private List<PackageInfo> selectedInstallerInfos = null;
75
    private Map<PackageInfo, String> zipEntriesMap = null;
76
    private List<String> defaultSelectedPackets = null;
77

    
78
    public Decompress() {
79

    
80
    }
81

    
82
    public void decompressPlugins(InputStream is, File outputDirectory)
83
        throws InstallPackageServiceException {
84
        option = OPTION_DECOMPRESS;
85
        this.outputDirectory = outputDirectory;
86

    
87
        decompressFolderOfPlugins(is);
88
    }
89

    
90
    public void readPackageSetInstallInfos(InputStream is,
91
        List<PackageInfo> installerInfos, Map<PackageInfo, String> zipEntriesMap)
92
        throws InstallPackageServiceException {
93
        option = OPTION_READ_INSTALLINFO;
94
        this.readedIinstallerInfos = installerInfos;
95
        this.zipEntriesMap = zipEntriesMap;
96
        decompressFolderOfPlugins(is);
97
    }
98

    
99
    public void readPackageInstallInfo(InputStream is,
100
        List<PackageInfo> installerInfos,
101
        Map<PackageInfo, String> zipEntriesMap, String name)
102
        throws InstallPackageServiceException {
103
        option = OPTION_READ_INSTALLINFO;
104
        this.readedIinstallerInfos = installerInfos;
105
        this.zipEntriesMap = zipEntriesMap;
106
        decompressFolderOfPluginFromPackage(is, name);
107
    }
108

    
109
    public void decompressPlugin(InputStream is, File outputDirectory)
110
        throws InstallPackageServiceException {
111
        try {
112
            option = OPTION_DECOMPRESS;
113
            this.outputDirectory = outputDirectory;
114
            decompressPlugin(is);
115
        } catch (Exception e) {
116
            throw new InstallPackageServiceException(
117
                "Error reading the plugin", e);
118
        }
119
    }
120

    
121
    public InputStream searchPlugin(InputStream is, String zipEntry)
122
        throws InstallPackageServiceException {
123
        ZipEntry entry = null;
124

    
125
        try {
126
            ZipInputStream zipInputStream = new ZipInputStream(is);
127

    
128
            while ((entry = zipInputStream.getNextEntry()) != null) {
129
                if (entry.getName().equals(zipEntry)) {
130
                    return zipInputStream;
131
                }
132
                zipInputStream.closeEntry();
133
            }
134
            zipInputStream.closeEntry();
135

    
136
            zipInputStream.close();
137
        } catch (Exception e) {
138
            throw new InstallPackageServiceException(
139
                "Error reading the plugin", e);
140
        }
141
        return null;
142
    }
143

    
144
    public void installFromStream(InputStream is, PackageInfo installerInfo)
145
        throws InstallPackageServiceException {
146
        option = OPTION_INSTALL;
147
        this.selectedInstallerInfos = new ArrayList<PackageInfo>();
148
        this.selectedInstallerInfos.add(installerInfo);
149
        decompressFolderOfPlugins(is);
150
    }
151

    
152
    public void installFromStream(InputStream is,
153
        List<PackageInfo> installerInfos) throws InstallPackageServiceException {
154
        option = OPTION_INSTALL;
155
        this.selectedInstallerInfos = installerInfos;
156
        decompressFolderOfPlugins(is);
157
    }
158

    
159
    private void decompressFolderOfPlugins(InputStream is)
160
        throws InstallPackageServiceException {
161
        ZipInputStream zipInputStream = new ZipInputStream(is);
162
        ZipEntry entry = null;
163

    
164
        try {
165
            while ((entry = zipInputStream.getNextEntry()) != null) {
166
                if (entry.getName().equals("defaultPackages")) {
167
                    int count;
168
                    byte data[] = new byte[BUFFER];
169
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
170

    
171
                    while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
172
                        out.write(data, 0, count);
173
                    }
174

    
175
                    String str = out.toString();
176
                    String lineas[] = str.split("\\n");
177
                    List<String> defaultPackagesList = new ArrayList<String>();
178

    
179
                    for (int i = 0; i < lineas.length; i++) {
180
                        defaultPackagesList.add(lineas[i]);
181
                    }
182

    
183
                    defaultSelectedPackets = defaultPackagesList;
184
                    out.flush();
185
                } else {
186
                    logger.debug("Extracting all Plugins, plugin: " + entry);
187
                    if (option == OPTION_INSTALL) {
188

    
189
                    } else {
190
                        if (option == OPTION_DECOMPRESS) {
191
                            decompressPlugin(zipInputStream);
192
                        } else
193
                            if (option == OPTION_READ_INSTALLINFO) {
194
                                readPlugin(zipInputStream, entry.getName());
195
                            }
196
                    }
197
                }
198
                zipInputStream.closeEntry();
199
            }
200
            zipInputStream.close();
201

    
202
        } catch (Exception e) {
203
            throw new InstallPackageServiceException(
204
                "Error reading the plugin", e);
205
        }
206
    }
207
    
208
    private void decompressFolderOfPluginFromPackage(InputStream is, String name)
209
        throws InstallPackageServiceException {
210

    
211
        try {
212
            if (option == OPTION_INSTALL) {
213

    
214
            } else {
215
                if (option == OPTION_DECOMPRESS) {
216
                    decompressPlugin(is);
217
                } else {
218
                    if (option == OPTION_READ_INSTALLINFO) {
219
                        readPlugin(is, name);
220
                    }
221
                }
222
            }
223

    
224
        } catch (Exception e) {
225
            throw new InstallPackageServiceException(
226
                "Error reading the plugin", e);
227
        }
228
    }
229
    
230
    private void readPlugin(InputStream is, String zipEntryName)
231
        throws ZipException, IOException, InstallerInfoFileException {
232
        ZipEntry entry = null;
233
        int installerInfoNumber = zipEntriesMap.size();
234
        String packageInfoName = getPackageInfoFileName();
235
        String unixPackageInfoPath = "/".concat(packageInfoName);
236
        String windowsPackageInfoPath = "\\".concat(packageInfoName);
237
        ZipInputStream zis = new ZipInputStream(is);
238
        while ((entry = zis.getNextEntry()) != null) {
239
            logger.debug("Extracting: " + entry.getName());
240

    
241
            String name = entry.getName();
242
            // Just in case, but we are going to create them always using
243
            // the unix file separator
244
            if (name.endsWith(unixPackageInfoPath)
245
                || name.endsWith(windowsPackageInfoPath)) {
246
                PackageInfo installerInfo = readInstallInfo(zis);
247
                zipEntriesMap.put(installerInfo, zipEntryName);
248
                readedIinstallerInfos.add(installerInfo);
249
            }
250
            zis.closeEntry();
251
        }
252
        // Don't close the stream as if it is a zip file contained 
253
        // into another zip file, closing of the child Zip?nputStream
254
        // will close also the parent's.
255
//        zis.close();
256

    
257
        if (installerInfoNumber == zipEntriesMap.size()) {
258
            PackageInfo installerInfo = new DefaultPackageInfo();
259
            installerInfo.setCode(zipEntryName);
260
            installerInfo.setName(zipEntryName);
261
            zipEntriesMap.put(installerInfo, zipEntryName);
262
            readedIinstallerInfos.add(installerInfo);
263
        }
264
    }
265

    
266
    private void decompressPlugin(InputStream inputStream)
267
        throws ZipException, IOException, InstallerInfoFileException {
268
        ZipInputStream zis = null;
269
        ZipEntry entry = null;
270
        byte data[] = new byte[BUFFER];
271
        int count = 0;
272

    
273
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
274
        SimpleTaskStatus taskStatus =
275
            manager.creteDefaultSimpleTaskStatus("Uncompressing...");
276
        manager.add(taskStatus);
277
        long readed = 0;
278

    
279
        // // First read all the contents size
280
        // zis = new ZipInputStream(inputStream);
281
        // while ((entry = zis.getNextEntry()) != null) {
282
        // count += entry.getSize();
283
        // }
284
        // // zis.close();
285
        // taskStatus.setRangeOfValues(0, count);
286

    
287
        // Return the stream to the initial position
288
        zis = new ZipInputStream(inputStream);
289
        while ((entry = zis.getNextEntry()) != null) {
290
            String entryName = entry.getName();
291
            taskStatus.message(entryName);
292

    
293
            if (File.separatorChar != '/') {
294
                entryName = entryName.replace('/', File.separatorChar);
295
            }
296
            logger.debug("Extracting: " + entryName);
297

    
298
            File file =
299
                new File(outputDirectory.getAbsolutePath() + File.separator
300
                    + entryName);
301
            if (file.exists()) {
302
                delete(file);
303
            }
304
            if (entry.isDirectory()) {
305
                file.mkdirs();
306
            } else {
307
                createParentFolder(file);
308
                FileOutputStream fos = new FileOutputStream(file);
309
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
310
                    fos.write(data, 0, count);
311
                    readed += count;
312
                    taskStatus.setCurValue(readed);
313
                }
314
                fos.flush();
315
                fos.close();
316
            }
317
        }
318
        zis.close();
319
        taskStatus.remove();
320
    }
321

    
322
    private void createParentFolder(File file) {
323
        File parentFile = file.getParentFile();
324
        if (!parentFile.exists()) {
325
            parentFile.mkdirs();
326
        }
327
    }
328

    
329
    public boolean delete(File dir) {
330
        if (dir.isDirectory()) {
331
            String[] children = dir.list();
332
            for (int i = 0; i < children.length; i++) {
333
                boolean success = delete(new File(dir, children[i]));
334
                if (!success) {
335
                    return false;
336
                }
337
            }
338
        }
339
        return dir.delete();
340
    }
341

    
342
    public PackageInfo readInstallerInfo(InputStream is)
343
        throws InstallerInfoFileException {
344
        try {
345
            return readInstallInfo(new ZipInputStream(is));
346
        } catch (IOException e) {
347
            throw new InstallerInfoFileException("error_reading_installerinfo",
348
                e);
349
        }
350
    }
351

    
352
    private PackageInfo readInstallInfo(ZipInputStream zipInputStream)
353
        throws IOException, InstallerInfoFileException {
354
        int count;
355
        byte data[] = new byte[BUFFER];
356

    
357
        ByteArrayOutputStream out = new ByteArrayOutputStream();
358

    
359
        while ((count = zipInputStream.read(data, 0, BUFFER)) != -1) {
360
            out.write(data, 0, count);
361
        }
362
        out.flush();
363

    
364
        DefaultPackageInfo installerInfo = new DefaultPackageInfo();
365
        InstallerInfoFileReader installerInfoFileReader =
366
            new InstallerInfoFileReader();
367
        installerInfoFileReader.read(installerInfo, new ByteArrayInputStream(
368
            out.toByteArray()));
369

    
370
        return installerInfo;
371
    }
372

    
373
    private String getPackageInfoFileName() {
374
        return InstallerProviderLocator.getProviderManager()
375
            .getPackageInfoFileName();
376
    }
377
    
378
    public List<String> getDefaultSelectedPackages() {
379
        return defaultSelectedPackets;
380
    }
381
}