从HTTP地址下载.zip文件 解析GZIP文件

摘要:
1packagecom.example.zipjiexitest;23importjava.io.BufferedInputStream;4importjava.io.DataInputStream;5importjava.io.File;6importjava.io.FileInputStream;7importjava.io.FileNotFoundException;8importjava.
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 }
View Code

java 压缩解压缩技术

http://snowolf.iteye.com/blog/642492

免责声明:文章转载自《从HTTP地址下载.zip文件 解析GZIP文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ArcMap操作随记(10)(转)SQL按照日、周、月、年统计数据下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

.Net Core System.IO.Compression.ZipFile实现Zip格式压缩和Zip格式解压缩

一、安装Nuget包 System.IO.Compression.ZipFile Install-package System.IO.Compression.ZipFile 二、ZipFile 类使用 简单操作方法: ZipFile.CreateFromDirectory()---压缩 ZipFile.ExtractToDirectory()---解压缩...

Ubuntu下如何解压缩zip,tar,tar.gz,tar.bz2文件

转自:http://wangli-5665.diandian.com/post/2011-08-18/4039228 这么多年来,数据压缩对我们来说是非常有用的。无论是在邮件中发送的图片用的zip文件还是在服务器压缩数据文件,我们都可以让下载更容易或者有效 的节约磁盘空间。某些压缩格式有时允许我们以60%的比率(甚至更高)压缩文件。下面我将会给大家演示如何...

winzip-dos

http://blog.chinaunix.net/uid-74941-id-155436.html 原文 使用winzip命令行对文件打包压缩 大家都知道winzip对文件解压和压缩都易如反掌,但是如何通过程序和命令行对其调用呢?去http://www.winzip.com/downcl.htm 下载dos版的winzip,下载后直接安装,就会在winz...

创建.ZIP压缩文件[CL_ABAP_ZIP]

SAP提供了一个类CL_ABAP_ZIP来创建.zip扩展名的压缩文件。 代码: 先将文件通过cl_gui_frontend_services=>gui_upload以BIN的文件类型上载,然后通过cl_abap_zip中的方法add() save()压缩,最后下载到本地。   1 2 3 4 5 6 7 8 9 10 11 12 13 1...

Linux下添加php的zip模块

./configure --with-php-config=/usr/local/php/bin/php-config extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/zip.so Linux下添加php的zip模块 今天早上开发的人员过来跟我说,测试机上的XX项目...

web一次下载多个附件

摘要 我们的系统有附件功能,在下载附件时,有时会下载多个附件,系统采用了把多个附件一起打包成zip文件下载的方式,这样下载过来是个压缩包,客户最近提出,他们不会解压缩这个压缩包,或者客户机器上根本就没有安装压缩软件,客户感觉使用起来很不方便(没办法,这种需求也真够空前的),好吧,谁让上帝开口了呢,一个字“做”。 思路 思路1:每次点击下载的时候启动多个&l...