Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: Java  |  size: 2.98 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  private Thread thread = new Thread()
  2.     {
  3.  
  4.         @Override
  5.         public void run()
  6.         {
  7.             sendMessage("-----------------------------------------------");
  8.  
  9.             // Create a directory in the SDCard to store the files
  10.             File file = new File(ROOT_FOLDER);
  11.             if (!file.exists())
  12.             {
  13.                 file.mkdirs();
  14.             }
  15.             else
  16.             {
  17.                 file.delete();
  18.             }
  19.             try
  20.             {
  21.                 // Open the ZipInputStream
  22.                 ZipInputStream inputStream = new ZipInputStream(getAssets().open("ZipTest.zip"));
  23.  
  24.                 // Loop through all the files and folders
  25.                 for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream
  26.                         .getNextEntry())
  27.                 {
  28.                     sendMessage("Extracting: " + entry.getName() + "...");
  29.  
  30.                     String innerFileName = ROOT_FOLDER + File.separator + entry.getName();
  31.                     File innerFile = new File(innerFileName);
  32.                     if (innerFile.exists())
  33.                     {
  34.                         innerFile.delete();
  35.                     }
  36.  
  37.                     // Check if it is a folder
  38.                     if (entry.isDirectory())
  39.                     {
  40.                         // Its a folder, create that folder
  41.                         innerFile.mkdirs();
  42.                     }
  43.                     else
  44.                     {
  45.                         // Create a file output stream
  46.                         FileOutputStream outputStream = new FileOutputStream(innerFileName);
  47.                         final int BUFFER = 2048;
  48.  
  49.                         // Buffer the ouput to the file
  50.                         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream,
  51.                                 BUFFER);
  52.  
  53.                         // Write the contents
  54.                         int count = 0;
  55.                         byte[] data = new byte[BUFFER];
  56.                         while ((count = inputStream.read(data, 0, BUFFER)) != -1)
  57.                         {
  58.                             bufferedOutputStream.write(data, 0, count);
  59.                         }
  60.  
  61.                         // Flush and close the buffers
  62.                         bufferedOutputStream.flush();
  63.                         bufferedOutputStream.close();
  64.                     }
  65.                     sendMessage("DONE");
  66.                    
  67.                     // Close the current entry
  68.                     inputStream.closeEntry();
  69.                 }
  70.                 inputStream.close();
  71.                 sendMessage("-----------------------------------------------");
  72.                 sendMessage("Unzipping complete");
  73.  
  74.             }
  75.             catch (IOException e)
  76.             {
  77.                 sendMessage("Exception occured: " + e.getMessage());
  78.                 e.printStackTrace();
  79.             }
  80.         }
  81.  
  82.     };