Thanks :-)

On Jul 17, 10:49 pm, nation-x <[email protected]> wrote:
> Here you go. :)
>
> static Handler myHandler;
> ProgressDialog myProgress;
>
> public void unzipFile(File zipfile) {
>         myProgress = ProgressDialog.show(getContext(), "Extract Zip",
>                         "Extracting Files...", true, false);
>         File zipFile = zipfile;
>         String directory = null;
>         directory = zipFile.getParent();
>         directory = directory + "/";
>         myHandler = new Handler() {
>
>                 @Override
>                 public void handleMessage(Message msg) {
>                         // process incoming messages here
>                         switch (msg.what) {
>                         case 0:
>                                 // update progress bar
>                                 myProgress.setMessage("" + (String) msg.obj);
>                                 break;
>                         case 1:
>                                 myProgress.cancel();
>                                 Toast toast = Toast.makeText(getContext(),
>                                                 "Zip extracted successfully", 
> Toast.LENGTH_SHORT);
>                                 toast.show();
>                                 provider.refresh();
>                                 break;
>                         case 2:
>                                 myProgress.cancel();
>                                 break;
>                         }
>                         super.handleMessage(msg);
>                 }
>
>         };
>         Thread workthread = new Thread(new UnZip(zipFile, directory));
>         workthread.start();
>
> }
>
> public class UnZip implements Runnable {
>
>         File archive;
>         String outputDir;
>
>         public UnZip(File ziparchive, String directory) {
>                 archive = ziparchive;
>                 outputDir = directory;
>         }
>
>         public void log(String log) {
>                 Log.v("unzip", log);
>         }
>
>         @SuppressWarnings("unchecked")
>         public void run() {
>                 Message msg;
>                 try {
>                         ZipFile zipfile = new ZipFile(archive);
>                         for (Enumeration e = zipfile.entries(); 
> e.hasMoreElements();) {
>                                 ZipEntry entry = (ZipEntry) e.nextElement();
>                                 msg = new Message();
>                                 msg.what = 0;
>                                 msg.obj = "Extracting " + entry.getName();
>                                 myHandler.sendMessage(msg);
>                                 unzipEntry(zipfile, entry, outputDir);
>                         }
>                 } catch (Exception e) {
>                         log("Error while extracting file " + archive);
>                 }
>                 msg = new Message();
>                 msg.what = 1;
>                 myHandler.sendMessage(msg);
>         }
>
>         @SuppressWarnings("unchecked")
>         public void unzipArchive(File archive, String outputDir) {
>                 try {
>                         ZipFile zipfile = new ZipFile(archive);
>                         for (Enumeration e = zipfile.entries(); 
> e.hasMoreElements();) {
>                                 ZipEntry entry = (ZipEntry) e.nextElement();
>                                 unzipEntry(zipfile, entry, outputDir);
>                         }
>                 } catch (Exception e) {
>                         log("Error while extracting file " + archive);
>                 }
>         }
>
>         private void unzipEntry(ZipFile zipfile, ZipEntry entry,
>                         String outputDir) throws IOException {
>
>                 if (entry.isDirectory()) {
>                         createDir(new File(outputDir, entry.getName()));
>                         return;
>                 }
>
>                 File outputFile = new File(outputDir, entry.getName());
>                 if (!outputFile.getParentFile().exists()) {
>                         createDir(outputFile.getParentFile());
>                 }
>
>                 log("Extracting: " + entry);
>                 BufferedInputStream inputStream = new 
> BufferedInputStream(zipfile
>                                 .getInputStream(entry));
>                 BufferedOutputStream outputStream = new BufferedOutputStream(
>                                 new FileOutputStream(outputFile));
>
>                 try {
>                         IOUtils.copy(inputStream, outputStream);
>                 } finally {
>                         outputStream.close();
>                         inputStream.close();
>                 }
>         }
>
>         private void createDir(File dir) {
>                 log("Creating dir " + dir.getName());
>                 if (!dir.mkdirs())
>                         throw new RuntimeException("Can not create dir " + 
> dir);
>         }
>
> }
>
> Shawn McAllister
>
> On Jul 15, 6:53 am,Mystique<[email protected]> wrote:
>
>
>
> > Hi, I'm starting out and trying to unzip a zip file back to source.
> > I'm using this code, my source is call "/mnt/sdcard/Out.zip" to output
> > "/mnt/sdcard/Out.txt" and it seems to work.
> > Now, if I have many files in Out.zip how do I automatically unzip all
> > files individually with the correct filename without creating Out.txt
> > and then write onto it?
>
> > ---code---
> > try    {
> >     File inFolder=new File("/mnt/sdcard/Out.zip");
> >     BufferedOutputStream out = null;
> >     ZipInputStream  in = new ZipInputStream(new
> > BufferedInputStream(new FileInputStream(inFolder)));
> >     int count;
> >     byte data[] = new byte[1000];
> >     out = new BufferedOutputStream(new FileOutputStream("/mnt/sdcard/
> > Out.txt"),1000);
>
> >     while ((count = in.read(data,0,1000)) != -1) {
> >     out.write(data,0,count);
> >     }
> >     out.flush();
> >     out.close();}
>
> > ---code---

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to