Skip to content

Commit 33ee204

Browse files
author
Nilanchala
committed
added zip manager
added zip manager
1 parent be41345 commit 33ee204

File tree

11 files changed

+251
-0
lines changed

11 files changed

+251
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.zipp"
4+
android:versionCode="1"
5+
android:versionName="1.0">
6+
<uses-sdk android:minSdkVersion="8" />
7+
8+
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
9+
<activity android:name="MainActivity"
10+
android:label="@string/app_name">
11+
<intent-filter>
12+
<action android:name="android.intent.action.MAIN" />
13+
<category android:name="android.intent.category.LAUNCHER" />
14+
</intent-filter>
15+
</activity>
16+
17+
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
18+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
19+
</application>
20+
21+
22+
</manifest>
18 KB
Loading
23 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system use,
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
10+
# Project target.
11+
target=android-8
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="fill_parent"
4+
android:layout_height="fill_parent"
5+
android:orientation="vertical" >
6+
7+
<TextView
8+
android:layout_width="fill_parent"
9+
android:layout_height="wrap_content"
10+
android:gravity="center_horizontal"
11+
android:padding="10dp"
12+
android:text="@string/instruction"
13+
android:textAppearance="?android:attr/textAppearanceLarge" />
14+
15+
<Button
16+
android:id="@+id/button_zip"
17+
android:layout_width="match_parent"
18+
android:layout_height="wrap_content"
19+
android:padding="10dp"
20+
android:text="Create Zip" />
21+
22+
<Button
23+
android:id="@+id/button_unzip"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content"
26+
android:padding="10dp"
27+
android:text="Unzip" />
28+
29+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<resources>
3+
<string name="instruction">Zipping and Unzipping!</string>
4+
<string name="app_name">User Application</string>
5+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.zipp;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileOutputStream;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import android.app.Activity;
10+
import android.os.Bundle;
11+
import android.os.Environment;
12+
import android.util.Log;
13+
import android.view.View;
14+
import android.view.View.OnClickListener;
15+
import android.widget.Button;
16+
17+
public class MainActivity extends Activity {
18+
19+
String inputPath = Environment.getExternalStorageDirectory().getPath()+ "/ZipDemo/";
20+
String inputFile = "Apply.zip";
21+
String outputPath = Environment.getExternalStorageDirectory().getPath()+ "/UnZipDemo/";
22+
23+
@Override
24+
public void onCreate(Bundle savedInstanceState) {
25+
super.onCreate(savedInstanceState);
26+
setContentView(R.layout.main);
27+
28+
((Button) findViewById(R.id.button_zip))
29+
.setOnClickListener(new OnClickListener() {
30+
31+
@Override
32+
public void onClick(View view) {
33+
// declare an array for storing the files i.e the path
34+
// of your source files
35+
String[] s = new String[2];
36+
37+
// Type the path of the files in here
38+
s[0] = inputPath + "/image.jpg";
39+
s[1] = inputPath + "/textfile.txt"; // /sdcard/ZipDemo/textfile.txt
40+
41+
// first parameter is d files second parameter is zip
42+
// file name
43+
ZipManager zipManager = new ZipManager();
44+
45+
// calling the zip function
46+
zipManager.zip(s, inputPath + inputFile);
47+
}
48+
});
49+
50+
((Button) findViewById(R.id.button_unzip))
51+
.setOnClickListener(new OnClickListener() {
52+
53+
@Override
54+
public void onClick(View view) {
55+
ZipManager zipManager = new ZipManager();
56+
zipManager.unzip(inputPath + inputFile, outputPath);
57+
}
58+
});
59+
60+
//moveFile(inputPath, inputFile, outputPath);
61+
62+
}
63+
64+
private void moveFile(String inputPath, String inputFile, String outputPath) {
65+
66+
InputStream in = null;
67+
OutputStream out = null;
68+
try {
69+
// create output directory if it doesn't exist
70+
File dir = new File(outputPath);
71+
if (!dir.exists()) {
72+
dir.mkdirs();
73+
}
74+
75+
in = new FileInputStream(inputPath + inputFile);
76+
out = new FileOutputStream(outputPath + inputFile);
77+
78+
byte[] buffer = new byte[1024];
79+
int read;
80+
while ((read = in.read(buffer)) != -1) {
81+
out.write(buffer, 0, read);
82+
}
83+
in.close();
84+
in = null;
85+
86+
// write the output file
87+
out.flush();
88+
out.close();
89+
out = null;
90+
91+
// delete the original file
92+
new File(inputPath + inputFile).delete();
93+
}
94+
95+
catch (FileNotFoundException fnfe1) {
96+
Log.e("tag", fnfe1.getMessage());
97+
} catch (Exception e) {
98+
Log.e("tag", e.getMessage());
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.zipp;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.util.zip.ZipEntry;
9+
import java.util.zip.ZipInputStream;
10+
import java.util.zip.ZipOutputStream;
11+
import android.util.Log;
12+
13+
public class ZipManager {
14+
private static final int BUFFER = 80000;
15+
16+
public void zip(String[] _files, String zipFileName) {
17+
try {
18+
BufferedInputStream origin = null;
19+
FileOutputStream dest = new FileOutputStream(zipFileName);
20+
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
21+
dest));
22+
byte data[] = new byte[BUFFER];
23+
24+
for (int i = 0; i < _files.length; i++) {
25+
Log.v("Compress", "Adding: " + _files[i]);
26+
FileInputStream fi = new FileInputStream(_files[i]);
27+
origin = new BufferedInputStream(fi, BUFFER);
28+
29+
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
30+
out.putNextEntry(entry);
31+
int count;
32+
33+
while ((count = origin.read(data, 0, BUFFER)) != -1) {
34+
out.write(data, 0, count);
35+
}
36+
origin.close();
37+
}
38+
39+
out.close();
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
public void unzip(String _zipFile, String _targetLocation) {
46+
47+
//create target location folder if not exist
48+
dirChecker(_targetLocation);
49+
50+
try {
51+
FileInputStream fin = new FileInputStream(_zipFile);
52+
ZipInputStream zin = new ZipInputStream(fin);
53+
ZipEntry ze = null;
54+
while ((ze = zin.getNextEntry()) != null) {
55+
56+
//create dir if required while unzipping
57+
if (ze.isDirectory()) {
58+
dirChecker(ze.getName());
59+
} else {
60+
FileOutputStream fout = new FileOutputStream(_targetLocation + ze.getName());
61+
for (int c = zin.read(); c != -1; c = zin.read()) {
62+
fout.write(c);
63+
}
64+
65+
zin.closeEntry();
66+
fout.close();
67+
}
68+
69+
}
70+
zin.close();
71+
} catch (Exception e) {
72+
System.out.println(e);
73+
}
74+
}
75+
76+
private void dirChecker(String dir) {
77+
File f = new File(dir);
78+
if (!f.isDirectory()) {
79+
f.mkdirs();
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)