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
+ }
0 commit comments