2

Which library I have to use to work with archives(like rar, zip) on android. Some sample code. I can't find any example of archiving files.

Lang
  • 131
  • 1
  • 9
  • This has been asked before [here for rar](http://stackoverflow.com/questions/561107/rar-archives-with-java), and [here for zip](http://stackoverflow.com/questions/9558595/android-library-zip). Please try to search first. – RivieraKid Mar 20 '12 at 09:41
  • Possible duplicate of [RAR archives with java](https://stackoverflow.com/questions/561107/rar-archives-with-java) – Brandon Minnick Aug 30 '17 at 00:03

4 Answers4

2

Are you zipping or unzipping? (or both?) Previously I’ve used ZipInputStream. The tactic you use may depend on where the Zip is stored (on the SD Card / in the Assets of the APK / in an APK Extension). If its an asset, for example, you can use AssetManager to open a file as an InputSteam. If it’s on the SD card, you might want to use ZipFile.

There's a Java tutorial here that may help: http://java.sun.com/developer/technicalArticles/Programming/compression/

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
HaemEternal
  • 2,229
  • 6
  • 31
  • 50
  • I want work with archived files without unzipping to SDcard. I have archive with files and i want work with this files. – Lang Mar 20 '12 at 09:54
  • So are you looking to investigate the contents without extracting anything? – HaemEternal Mar 20 '12 at 09:57
  • ZipFile is usually best if you want random access (to grab a specific file out of your zip very quickly). ZipInputStream may be better if you working with streams anyway (like if you are using AssetManager). It sounds like either method will work fine for what you're doing. – HaemEternal Mar 20 '12 at 10:14
1

see this example

  public void unzip() {
                   try  {
                     FileInputStream fin = new FileInputStream(_zipFile);
                     ZipInputStream zin = new ZipInputStream(fin);
                     ZipEntry ze = null;
                     while ((ze = zin.getNextEntry()) != null) {
                       Log.v("Decompress", "Unzipping " + ze.getName());
                       System.out.println("^^^^^^UnzippingFile^"+ze.getName());
                       ///code to search is given string exists or not in a Sentence
                       String haystack = ze.getName();
                       String needle1 = ".DS_Store";
                       int index1 = haystack.indexOf(needle1);
                       if (index1 != -1)
                       {
                           System.out.println("The string contains the substring "
+ needle1);
                           continue;
                       }
                       /*else
                           System.out.println("The string does not contain the
substring " + needle1);*/


                       if(ze.isDirectory()) {
                         _dirChecker(ze.getName());
                       } else {
                         FileOutputStream fout = new FileOutputStream(_location +
ze.getName());
                      // replace for loop with:
                         byte[] buffer = new byte[1024];
                         int length;
                         while ((length = zin.read(buffer))>0) {
                         fout.write(buffer, 0, length);
                         }

                         zin.closeEntry();
                         fout.close();
                       }




                     }////Outer While
                     zin.close();
                   } catch(Exception e) {
                     Log.e("Decompress", "unzip", e);
                   }

                 }

                 private void _dirChecker(String dir) {
                   File f = new File(_location + dir);

                   if(!f.isDirectory()) {
                     f.mkdirs();
                   }
                 }
Venkata Krishna
  • 1,543
  • 1
  • 11
  • 19
0

for zip you can use java.util.Zip, for rar archive i think you have to go through third party library

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
-4

Right Click on your project---> Properties --->Java Build path ----> Libraries---> Add External Jar then add libraries in your project in any format .zip or .rar

Nikhil Lamba
  • 593
  • 1
  • 6
  • 17