PageRenderTime 26ms CodeModel.GetById 8ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/WebAccess/src/com/ideal/webaccess/Unzipper.java

http://eyes-free.googlecode.com/
Java | 93 lines | 66 code | 9 blank | 18 comment | 9 complexity | 8a0a29de47e908fc863707e9f7273bab MD5 | raw file
 1/*
 2 * Copyright (C) 2010 The IDEAL Group
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 *      http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.ideal.webaccess;
18
19import android.os.Environment;
20
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.util.zip.ZipEntry;
26import java.util.zip.ZipInputStream;
27
28/**
29 * Utility class for unzipping the JavaScript files.
30 */
31public class Unzipper {
32    public static boolean unzip(InputStream stream) {
33        String rootDirectory = Environment.getExternalStorageDirectory() + "/";
34        FileOutputStream out;
35        byte buf[] = new byte[16384];
36        try {
37            ZipInputStream zis = new ZipInputStream(stream);
38            ZipEntry entry = zis.getNextEntry();
39            while (entry != null) {
40                if (entry.isDirectory()) {
41                    File newDir = new File(rootDirectory + entry.getName());
42                    newDir.mkdir();
43                } else {
44                    String name = entry.getName();
45                    File outputFile = new File(rootDirectory + name);
46                    String outputPath = outputFile.getCanonicalPath();
47                    name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
48                    outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
49                    File outputDir = new File(outputPath);
50                    outputDir.mkdirs();
51                    outputFile = new File(outputPath, name);
52                    outputFile.createNewFile();
53                    out = new FileOutputStream(outputFile);
54
55                    int numread = 0;
56                    do {
57                        numread = zis.read(buf);
58                        if (numread <= 0) {
59                            break;
60                        } else {
61                            out.write(buf, 0, numread);
62                        }
63                    } while (true);
64                    out.close();
65                }
66                entry = zis.getNextEntry();
67            }
68            return true;
69        } catch (IOException e) {
70            e.printStackTrace();
71            return false;
72        }
73
74    }
75
76    public static boolean doDataCheckAndUnzip(InputStream dataSourceStream) {
77        final String basePath = Environment.getExternalStorageDirectory() + "/ideal-webaccess/js/";
78
79        final String[] dataFiles = {
80                "clc-domUtils.js", "ideal-dom.js", "ideal-globals.js", "ideal-interface.js",
81                "ideal-keyhandler.js", "ideal-lens.js", "ideal-loader.js",
82                "ideal-loader_webreader.js", "ideal-nav.js", "ideal-styler.js", "ideal-tts.js",
83                "ideal-webaccess.user.js", "sitescripts/googlesearch/mgws.js"
84        };
85
86        for (int i = 0; i < dataFiles.length; i++) {
87            if (!(new File(basePath + dataFiles[i])).exists()) {
88                return unzip(dataSourceStream);
89            }
90        }
91        return true;
92    }
93}