从HTTP地址下载.zip文件 解析GZIP文件
2013-11-29 17:52
260 查看
package com.example.zipjiexitest; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class ZIPActivity extends Activity { private TextView tv; private String str; private Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zip); tv=(TextView)findViewById(R.id.tv); // unbyte(); // new Thread(thread).start(); try { this.zipdecompress(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } handler=new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); tv.setText(str); } }; } private Runnable thread=new Runnable(){ public void run(){ try { getZip(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.sendMessage(handler.obtainMessage()); } }; @SuppressLint("SdCardPath") private void getZip() throws IOException{ String strurl="http://172.16.2.10:16834/hqApplet/data/day/00TSLA.day.zip"; URL url = null; int line=0; try { url=new URL(strurl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } HttpURLConnection httpurl=(HttpURLConnection) url.openConnection(); httpurl.connect(); String filename="/sdcard/abc.zip"; BufferedInputStream bis=new BufferedInputStream(httpurl.getInputStream()); FileOutputStream fos=new FileOutputStream(filename); byte[] buf=new byte[8096]; while ((line = bis.read(buf)) != -1) { fos.write(buf,0,line); } fos.close(); bis.close(); httpurl.disconnect(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_zip, menu); return true; } /* * 这个是解压ZIP格式文件的方法 * * @zipFileName:是传进来你要解压的文件路径,包括文件的名字; * * @outputDirectory:选择你要保存的路劲; * */ private void unbyte(){ try { DataInputStream dis=new DataInputStream(new FileInputStream(new File("/sdcard/abc"))); int zip=dis.readInt(); System.out.println("-------------"+zip); for(int i=0;i<zip;i++){ int date=dis.readInt()+19970000; System.out.println("-------------"+date); float f1=dis.readFloat(); float f2=dis.readFloat(); float f3=dis.readFloat(); float f4=dis.readFloat(); float f5=dis.readFloat(); long l=dis.readLong(); float f6=dis.readFloat(); int it=dis.readInt(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private String TAG="zip----------------"; private void unzip() throws Exception { ZipFile zf=new ZipFile("/sdcard/abc.zip"); System.out.println("--------------"+zf.size()); CheckedInputStream cis = new CheckedInputStream(new FileInputStream(new File("/sdcard/abc.zip")), new CRC32()); ZipInputStream in = new ZipInputStream(cis); ZipEntry z; String name = ""; String extractedFile = ""; int counter = 0; Log.d(TAG, "unzipping file: " + 111111); while ((z = in.getNextEntry()) != null) { name = z.getName(); Log.d(TAG, "unzipping file: " + name); if (z.isDirectory()) { Log.d(TAG, name + "is a folder"); // get the folder name of the widget name = name.substring(0, name.length() - 1); File folder = new File("/sdcard/" + File.separator + name); folder.mkdirs(); if (counter == 0) { extractedFile = folder.toString(); } counter++; Log.d(TAG, "mkdir " + "/sdcard/" + File.separator + name); } else { Log.d(TAG, name + "is a normal file"); File file = new File("/sdcard/" + File.separator + name); file.createNewFile(); // get the output stream of the file FileOutputStream out = new FileOutputStream(file); int ch; byte[] buffer = new byte[1024]; // read (ch) bytes into buffer while ((ch = in.read(buffer)) != -1) { // write (ch) byte from buffer at the position 0 out.write(buffer, 0, ch); out.flush(); } out.close(); } } in.close(); } /** * 数据解压缩 * * @param is * @param os * @throws Exception */ @SuppressLint({ "SdCardPath", "SdCardPath" }) public static void zipdecompress() throws Exception { System.out.println("-------------zipdecompress"); GZIPInputStream gis = new GZIPInputStream(new FileInputStream(new File("/sdcard/abc.zip"))); File file=new File("/sdcard/zip"); file.createNewFile(); FileOutputStream fos=new FileOutputStream(file); int count; byte data[] = new byte[1024]; while ((count = gis.read(data, 0, 1024)) != -1) { fos.write(data, 0, count); } System.out.println("-------------zipdecompress"); gis.close(); } }
View Code
java 压缩解压缩技术
/article/4182787.html
相关文章推荐
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- java下载远程http地址的图片文件到本地-自动处理图片是否经过服务器gzip压缩的问题
- Android安卓根据地址下载文件并保存到本地(HttpDownload)
- http断点续传与文件下载原理解析
- 关于JS访问文件服务器的HTTP文件地址实现文件下载
- 关于CDH页面下载HDFS文件地址解析出错
- HTTP解读-文件下载、中文乱码、请求重定向、定时刷新、缓存、url编解码、gzip压缩、防盗链等
- C# Http地址下载文件到指定目录。
- java resteasy restful webservice教程(六):实现文件下载功能实例文章来源:爱上123 原文地址:http://www.ishang123.com/jishubowen/
- java按http地址列表下载文件队列
- 通过Http地址返回JSON数据,进行解析;通过ftp返回.txt文件数据,进行解析;
- 解析 Linux 中的 VFS 文件系统机制 http://www.ibm.com/developerworks/cn/linux/l-vfs/
- Python实现HTTP协议下的文件下载方法总结
- Android 从本地服务器下载文件与解析过程详解(三)
- HTTP 错误 404.3 – Not Found 由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
- Http多线程下载文件的处理机制
- 从原理角度解析Android (Java) http 文件上传
- sudoers文件解析 转自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1971013
- HttpClient使用之下载远程服务器中的文件(注意目录遍历漏洞)
- 下载网络文件HttpURLConnection.getContentLength()大小为 -1