Decompress the zip file in the Android program and load and display the decompressed file content.

Source: Internet
Author: User

 

I just created a demo to decompress the local zip file and use webview to display one of the html files and directly upload the code. If you need it, you can check it out.

 

Public class ZipActivity extends Activity {

Private static final String TAG = "HelloXmlActivity ";

Private WebView mWebView;

 

Private static LinkedHashMap <String, String> widgetInfoMap = new LinkedHashMap <String, String> ();

 

// Http://blog.csdn.net/com360/article/details/6618086

/** Called when the activity is first created .*/

@ Override

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

String zipfile = "/sdcard/abc.zip ";

Try {

Unzip (zipfile, "/sdcard/"); // yangguangfu/wujiali/

} Catch (Exception e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

MWebView = (WebView) findViewById (R. id. web );

MWebView. loadUrl ("file: // sdcard/abc/aaa.html"); // load the extracted html content here

 

}

 

/*

* This is the method for extracting ZIP files.

*

* @ ZipFileName: indicates the path of the file to be decompressed, including the file name;

*

* @ OutputDirectory: select the path you want to save;

*

*/

Private void unzip (String zipFileName, String outputDirectory)

Throws Exception {

ZipInputStream in = new ZipInputStream (new FileInputStream (zipFileName ));

ZipEntry z;

String name = "";

String extractedFile = "";

Int counter = 0;

 

While (z = in. getNextEntry ())! = Null ){

Name = z. getName ();

Log. d (TAG, "unzipping file:" + name );

If (z. isDirectory ()){

Log. d (TAG, name + "is a folder ");

// Get the folder name of the widget

Name = name. substring (0, name. length ()-1 );

File folder = new File (outputDirectory + File. separator + name );

Folder. mkdirs ();

If (counter = 0 ){

ExtractedFile = folder. toString ();

}

Counter ++;

Log. d (TAG, "mkdir" + outputDirectory + File. separator + name );

} Else {

Log. d (TAG, name + "is a normal file ");

File file = new File (outputDirectory + File. separator + name );

File. createNewFile ();

// Get the output stream of the file

FileOutputStream out = new FileOutputStream (file );

Int ch;

Byte [] buffer = new byte [1024];

// Read (ch) bytes into buffer

While (ch = in. read (buffer ))! =-1 ){

// Write (ch) byte from buffer at the position 0

Out. write (buffer, 0, ch );

Out. flush ();

}

Out. close ();

}

}

 

In. close ();

 

}

 

}

 

In this example, my abc.zip file is stored in sdcard, which contains two files. decompress the file and generate an abc folder. The folder contains two decompressed files, I used a webview to directly load an html file after learning about the compression, Which is rough, saving the file existence judgment, scanning the file name, file type, main. the xml file is also very simple. You can see the controls in the above Code. Here, the xml layout file is no longer written.

 

From andy Pan's column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.