/androidsays/src/com/google/marvin/androidsays/Unzipper.java

http://eyes-free.googlecode.com/ · Java · 116 lines · 82 code · 11 blank · 23 comment · 7 complexity · ab0edfd4ac9c3826c81a6db6d74499d7 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.marvin.androidsays;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.Enumeration;
  22. import java.util.zip.ZipEntry;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25. import java.net.URLConnection;
  26. import java.util.zip.ZipFile;
  27. /**
  28. * Unzips theme files for mem into the appropriate directory on the sdcard.
  29. *
  30. * @author clchen@google.com (Charles L. Chen)
  31. */
  32. public class Unzipper {
  33. public static String download(String fileUrl) {
  34. URLConnection cn;
  35. try {
  36. fileUrl = (new URL(new URL(fileUrl), fileUrl)).toString();
  37. URL url = new URL(fileUrl);
  38. cn = url.openConnection();
  39. cn.connect();
  40. InputStream stream = cn.getInputStream();
  41. File outputDir = new File("/sdcard/mem/memes/");
  42. outputDir.mkdirs();
  43. String filename = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
  44. filename = filename.substring(0, filename.indexOf("meme.zip") + 8);
  45. File outputFile = new File("/sdcard/mem/memes/", filename);
  46. outputFile.createNewFile();
  47. FileOutputStream out = new FileOutputStream(outputFile);
  48. byte buf[] = new byte[16384];
  49. do {
  50. int numread = stream.read(buf);
  51. if (numread <= 0) {
  52. break;
  53. } else {
  54. out.write(buf, 0, numread);
  55. }
  56. } while (true);
  57. stream.close();
  58. out.close();
  59. return "/sdcard/mem/memes/" + filename;
  60. } catch (MalformedURLException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. return "";
  64. } catch (IOException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. return "";
  68. }
  69. }
  70. public static void unzip(String fileUrl) {
  71. try {
  72. String filename = download(fileUrl);
  73. ZipFile zip = new ZipFile(filename);
  74. Enumeration<? extends ZipEntry> zippedFiles = zip.entries();
  75. while (zippedFiles.hasMoreElements()) {
  76. ZipEntry entry = zippedFiles.nextElement();
  77. InputStream is = zip.getInputStream(entry);
  78. String name = entry.getName();
  79. File outputFile = new File("/sdcard/mem/themes/" + name);
  80. String outputPath = outputFile.getCanonicalPath();
  81. name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
  82. outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
  83. File outputDir = new File(outputPath);
  84. outputDir.mkdirs();
  85. outputFile = new File(outputPath, name);
  86. outputFile.createNewFile();
  87. FileOutputStream out = new FileOutputStream(outputFile);
  88. byte buf[] = new byte[16384];
  89. do {
  90. int numread = is.read(buf);
  91. if (numread <= 0) {
  92. break;
  93. } else {
  94. out.write(buf, 0, numread);
  95. }
  96. } while (true);
  97. is.close();
  98. out.close();
  99. }
  100. } catch (IOException e) {
  101. // TODO Auto-generated catch block
  102. e.printStackTrace();
  103. }
  104. }
  105. }