1 packagecom.example.zipjiexitest; 2 3 importjava.io.BufferedInputStream; 4 importjava.io.DataInputStream; 5 importjava.io.File; 6 importjava.io.FileInputStream; 7 importjava.io.FileNotFoundException; 8 importjava.io.FileOutputStream; 9 importjava.io.IOException; 10 importjava.io.InputStream; 11 importjava.io.OutputStream; 12 importjava.net.HttpURLConnection; 13 importjava.net.MalformedURLException; 14 importjava.net.URL; 15 importjava.util.zip.CRC32; 16 importjava.util.zip.CheckedInputStream; 17 importjava.util.zip.GZIPInputStream; 18 importjava.util.zip.ZipEntry; 19 importjava.util.zip.ZipFile; 20 importjava.util.zip.ZipInputStream; 21 22 importandroid.annotation.SuppressLint; 23 importandroid.app.Activity; 24 importandroid.os.Bundle; 25 importandroid.os.Handler; 26 importandroid.os.Message; 27 importandroid.util.Log; 28 importandroid.view.Menu; 29 importandroid.widget.TextView; 30 31 public class ZIPActivity extendsActivity { 32 33 privateTextView tv; 34 privateString str; 35 privateHandler handler; 36 @Override 37 public voidonCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_zip); 40 tv=(TextView)findViewById(R.id.tv); 41 //unbyte(); 42 //new Thread(thread).start(); 43 try{ 44 this.zipdecompress(); 45 } catch(Exception e) { 46 //TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 handler=newHandler(){ 50 @Override 51 public voidhandleMessage(Message msg) { 52 //TODO Auto-generated method stub 53 super.handleMessage(msg); 54 tv.setText(str); 55 56 } 57 }; 58 } 59 private Runnable thread=newRunnable(){ 60 public voidrun(){ 61 try{ 62 getZip(); 63 } catch(IOException e) { 64 //TODO Auto-generated catch block 65 e.printStackTrace(); 66 } 67 handler.sendMessage(handler.obtainMessage()); 68 } 69 }; 70 71 @SuppressLint("SdCardPath") 72 private void getZip() throwsIOException{ 73 String strurl="http://172.16.2.10:16834/hqApplet/data/day/00TSLA.day.zip"; 74 URL url = null; 75 int line=0; 76 try{ 77 url=newURL(strurl); 78 } catch(MalformedURLException e) { 79 //TODO Auto-generated catch block 80 e.printStackTrace(); 81 } 82 HttpURLConnection httpurl=(HttpURLConnection) url.openConnection(); 83 httpurl.connect(); 84 String filename="/sdcard/abc.zip"; 85 86 BufferedInputStream bis=newBufferedInputStream(httpurl.getInputStream()); 87 FileOutputStream fos=newFileOutputStream(filename); 88 byte[] buf=new byte[8096]; 89 while ((line = bis.read(buf)) != -1) { 90 fos.write(buf,0,line); 91 } 92 fos.close(); 93 bis.close(); 94 httpurl.disconnect(); 95 96 } 97 @Override 98 public booleanonCreateOptionsMenu(Menu menu) { 99 getMenuInflater().inflate(R.menu.activity_zip, menu); 100 return true; 101 } 102 103 /* 104 * 这个是解压ZIP格式文件的方法 105 * 106 * @zipFileName:是传进来你要解压的文件路径,包括文件的名字; 107 * 108 * @outputDirectory:选择你要保存的路劲; 109 * 110 */ 111 private voidunbyte(){ 112 try{ 113 DataInputStream dis=new DataInputStream(new FileInputStream(new File("/sdcard/abc"))); 114 int zip=dis.readInt(); 115 System.out.println("-------------"+zip); 116 for(int i=0;i<zip;i++){ 117 int date=dis.readInt()+19970000; 118 System.out.println("-------------"+date); 119 float f1=dis.readFloat(); 120 float f2=dis.readFloat(); 121 float f3=dis.readFloat(); 122 float f4=dis.readFloat(); 123 float f5=dis.readFloat(); 124 long l=dis.readLong(); 125 float f6=dis.readFloat(); 126 int it=dis.readInt(); 127 } 128 129 130 } catch(FileNotFoundException e) { 131 //TODO Auto-generated catch block 132 e.printStackTrace(); 133 } catch(IOException e) { 134 //TODO Auto-generated catch block 135 e.printStackTrace(); 136 } 137 138 } 139 private String TAG="zip----------------"; 140 private voidunzip() 141 throwsException { 142 ZipFile zf=new ZipFile("/sdcard/abc.zip"); 143 System.out.println("--------------"+zf.size()); 144 CheckedInputStream cis = new CheckedInputStream(new FileInputStream(new File("/sdcard/abc.zip")), newCRC32()); 145 146 ZipInputStream in = newZipInputStream(cis); 147 ZipEntry z; 148 String name = ""; 149 String extractedFile = ""; 150 int counter = 0; 151 Log.d(TAG, "unzipping file: " + 111111); 152 while ((z = in.getNextEntry()) != null) { 153 name =z.getName(); 154 Log.d(TAG, "unzipping file: " +name); 155 if(z.isDirectory()) { 156 Log.d(TAG, name + "is a folder"); 157 //get the folder name of the widget 158 name = name.substring(0, name.length() - 1); 159 File folder = new File("/sdcard/" + File.separator +name); 160 folder.mkdirs(); 161 if (counter == 0) { 162 extractedFile =folder.toString(); 163 } 164 counter++; 165 Log.d(TAG, "mkdir " + "/sdcard/" + File.separator +name); 166 } else{ 167 Log.d(TAG, name + "is a normal file"); 168 File file = new File("/sdcard/" + File.separator +name); 169 file.createNewFile(); 170 //get the output stream of the file 171 FileOutputStream out = newFileOutputStream(file); 172 intch; 173 byte[] buffer = new byte[1024]; 174 //read (ch) bytes into buffer 175 while ((ch = in.read(buffer)) != -1) { 176 //write (ch) byte from buffer at the position 0 177 out.write(buffer, 0, ch); 178 out.flush(); 179 } 180 out.close(); 181 } 182 } 183 184 in.close(); 185 186 } 187 188 /** 189 * 数据解压缩 190 * 191 * @paramis 192 * @paramos 193 * @throwsException 194 */ 195 @SuppressLint({ "SdCardPath", "SdCardPath"}) 196 public static voidzipdecompress() 197 throwsException { 198 System.out.println("-------------zipdecompress"); 199 GZIPInputStream gis = new GZIPInputStream(new FileInputStream(new File("/sdcard/abc.zip"))); 200 File file=new File("/sdcard/zip"); 201 file.createNewFile(); 202 FileOutputStream fos=newFileOutputStream(file); 203 intcount; 204 byte data[] = new byte[1024]; 205 while ((count = gis.read(data, 0, 1024)) != -1) { 206 fos.write(data, 0, count); 207 } 208 System.out.println("-------------zipdecompress"); 209 gis.close(); 210 } 211 212 }
java 压缩解压缩技术