/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. package com.ideal.webaccess;
  17. import android.os.Environment;
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.zip.ZipEntry;
  23. import java.util.zip.ZipInputStream;
  24. /**
  25. * Utility class for unzipping the JavaScript files.
  26. */
  27. public class Unzipper {
  28. public static boolean unzip(InputStream stream) {
  29. String rootDirectory = Environment.getExternalStorageDirectory() + "/";
  30. FileOutputStream out;
  31. byte buf[] = new byte[16384];
  32. try {
  33. ZipInputStream zis = new ZipInputStream(stream);
  34. ZipEntry entry = zis.getNextEntry();
  35. while (entry != null) {
  36. if (entry.isDirectory()) {
  37. File newDir = new File(rootDirectory + entry.getName());
  38. newDir.mkdir();
  39. } else {
  40. String name = entry.getName();
  41. File outputFile = new File(rootDirectory + name);
  42. String outputPath = outputFile.getCanonicalPath();
  43. name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
  44. outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
  45. File outputDir = new File(outputPath);
  46. outputDir.mkdirs();
  47. outputFile = new File(outputPath, name);
  48. outputFile.createNewFile();
  49. out = new FileOutputStream(outputFile);
  50. int numread = 0;
  51. do {
  52. numread = zis.read(buf);
  53. if (numread <= 0) {
  54. break;
  55. } else {
  56. out.write(buf, 0, numread);
  57. }
  58. } while (true);
  59. out.close();
  60. }
  61. entry = zis.getNextEntry();
  62. }
  63. return true;
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. return false;
  67. }
  68. }
  69. public static boolean doDataCheckAndUnzip(InputStream dataSourceStream) {
  70. final String basePath = Environment.getExternalStorageDirectory() + "/ideal-webaccess/js/";
  71. final String[] dataFiles = {
  72. "clc-domUtils.js", "ideal-dom.js", "ideal-globals.js", "ideal-interface.js",
  73. "ideal-keyhandler.js", "ideal-lens.js", "ideal-loader.js",
  74. "ideal-loader_webreader.js", "ideal-nav.js", "ideal-styler.js", "ideal-tts.js",
  75. "ideal-webaccess.user.js", "sitescripts/googlesearch/mgws.js"
  76. };
  77. for (int i = 0; i < dataFiles.length; i++) {
  78. if (!(new File(basePath + dataFiles[i])).exists()) {
  79. return unzip(dataSourceStream);
  80. }
  81. }
  82. return true;
  83. }
  84. }