您的位置:首页 > 理论基础 > 计算机网络

从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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐