从HTTP地址下载.zip文件 解析GZIP文件
1 package com.example.zipjiexitest; 2 3 import java.io.BufferedInputStream; 4 import java.io.DataInputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.FileOutputStream; 9 import java.io.IOException; 10 import java.io.InputStream; 11 import java.io.OutputStream; 12 import java.net.HttpURLConnection; 13 import java.net.MalformedURLException; 14 import java.net.URL; 15 import java.util.zip.CRC32; 16 import java.util.zip.CheckedInputStream; 17 import java.util.zip.GZIPInputStream; 18 import java.util.zip.ZipEntry; 19 import java.util.zip.ZipFile; 20 import java.util.zip.ZipInputStream; 21 22 import android.annotation.SuppressLint; 23 import android.app.Activity; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.os.Message; 27 import android.util.Log; 28 import android.view.Menu; 29 import android.widget.TextView; 30 31 public class ZIPActivity extends Activity { 32 33 private TextView tv; 34 private String str; 35 private Handler handler; 36 @Override 37 public void onCreate(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=new Handler(){ 50 @Override 51 public void handleMessage(Message msg) { 52 // TODO Auto-generated method stub 53 super.handleMessage(msg); 54 tv.setText(str); 55 56 } 57 }; 58 } 59 private Runnable thread=new Runnable(){ 60 public void run(){ 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() throws IOException{ 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=new URL(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=new BufferedInputStream(httpurl.getInputStream()); 87 FileOutputStream fos=new FileOutputStream(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 boolean onCreateOptionsMenu(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 void unbyte(){ 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 void unzip() 141 throws Exception { 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")), new CRC32()); 145 146 ZipInputStream in = new ZipInputStream(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 = new FileOutputStream(file); 172 int ch; 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 * @param is 192 * @param os 193 * @throws Exception 194 */ 195 @SuppressLint({ "SdCardPath", "SdCardPath" }) 196 public static void zipdecompress() 197 throws Exception { 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=new FileOutputStream(file); 203 int count; 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 压缩解压缩技术