接到客户的需求,要求实现,ZIP文件夹解密,功能,找了已很久发现,这方面的资料确实很匮乏。贴出源码 和大家分享。
package com.hongan.test;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PassWordZipDemoActivity extends Activity {
/** Called when the activity is first created. */
Button btn_jiemi, btn_jiami;
ZipFileWithPassword zipFileWithPassword;
String filePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_jiemi=(Button) this.findViewById(R.id.jiemi);
btn_jiami=(Button) this.findViewById(R.id.jiami);
zipFileWithPassword=new ZipFileWithPassword();
filePath=ZipFileWithPassword.zipDir;
btn_jiemi.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
zipFileWithPassword=new ZipFileWithPassword();
zipFileWithPassword.newFolder();
zipFileWithPassword.TestDecryptZipFile();
}
});
btn_jiami.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
zipFileWithPassword.TestEncryptZipFile();
File file=new File(filePath);
Log.i("infor","filePath is"+ filePath);
deleteFile( file);
//file.delete();
}
});
}
private void deleteFile(File file){
if(file.exists()){
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
File files[] = file.listFiles();
for(int i=0;i<files.length;i++){
this.deleteFile(files[i]);
}
}
file.delete();
}else{
Log.i("infor","所删除的文件不存在!"+'\n');
}
}
}
Demo源码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
class HttpRequestBean {
private String ip;
private String realm;
private int port;
private String url;
private String params;
private boolean isPOST;
// getter 、setter方法略
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getRealm() {
return realm;
}
public void setRealm(String realm) {
this.realm = realm;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUrl() {
return url;
}
public void setUrl(/blog_article/String url/index.html) {
this.url = url;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
public boolean isPOST() {
return isPOST;
}
public void setPOST(boolean isPOST) {
this.isPOST = isPOST;
}
}
public class HttpSocketUtil {
private static String httpPOSTSocket(String ip, String realm, int port,
String url, String params) throws UnknownHostException, IOException {
Socket s = new Socket(ip, port);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s
.getOutputStream()));
if (url.startsWith("/")) {
url = url.substring(1);
}
int index2 = url.indexOf("?");
if (index2 > 0) {
url = url.substring(0, index2);
}
pw.println("POST " + url + " HTTP/1.1");
pw.println("Host:" + realm);
pw.println("Accept-Language: zh-cn,zh;q=0.5");
pw.println("Accept-Encoding: gzip,deflate");
pw.println("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7");
pw.println("Connection: keep-alive");
pw.println("Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK");
pw.println("Content-Type: application/x-www-form-urlencoded");
int leng = params.length();
pw.println("Content-Length: " + leng);
pw.println();
pw.println(params);
pw.println();
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String temp = null;
while((temp = br.readLine()) != "")
{
System.out.println(temp);
}
br.close();
pw.close();
s.close();
return temp;
}
public static String httpSocket(HttpRequestBean httpBean)
throws UnknownHostException, IOException {
if (httpBean.isPOST()) {
System.out.println("POST");
return httpPOSTSocket(httpBean);
} else {
System.out.println("GET...");
return httpGETSocket(httpBean);
}
}
private static String httpGETSocket(String ip, String realm, int port,
String url) throws UnknownHostException, IOException {
Socket s = new Socket(ip, port);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(s
.getOutputStream()));
if (url.startsWith("/")) {
url = url.substring(1);
}
pw.println("GET " + url + " HTTP/1.1");
pw.println("Host:" + realm);
pw.println("Accept-Language: zh-cn,zh;q=0.5");
pw.println("Accept-Encoding: gzip,deflate");
pw.println("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7");
pw.println("Connection: keep-alive");
// pw.println("Cookie: ASPSESSIONIDCSAATTCD=DOMMILABJOPANJPNNAKAMCPK");
pw.println("Content-Type: text/http");
pw.println();
pw.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(s
.getInputStream()));
String temp = null;
while((temp = br.readLine()) != "")
{
System.out.println(temp);
}
br.close();
pw.close();
s.close();
return temp;
}
private static String httpPOSTSocket(HttpRequestBean httpBean)
throws UnknownHostException, IOException {
return httpPOSTSocket(httpBean.getIp(), httpBean.getRealm(), httpBean
.getPort(), httpBean.getUrl(), httpBean.getParams());
}
private static String httpGETSocket(HttpRequestBean httpBean)
throws UnknownHostException, IOException {
return httpGETSocket(httpBean.getIp(), httpBean.getRealm(), httpBean
.getPort(), httpBean.getUrl());
}
public static void main(String[] args) {
String content = null;
HttpRequestBean httpBean = new HttpRequestBean();
String ip = "www.cnblogs.com";
httpBean.setIp(ip);
httpBean.setParams("tbUserName=123");
httpBean.setPort(80);
//httpBean.setUrl("http://www.cnblogs.com/");
httpBean.setUrl("http://passport.cnblogs.com/login.aspx");
httpBean.setPOST(true);
httpBean.setRealm(ip);
try {
content = httpSocket(httpBean);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(content);
}
1 首先我们还是创建一个Single View Application,然后打开MainStoryboard_iphone.storyboard,在IB中添加一个UISlider控件和一个Label,这个Label用来显示Slider的值。
选中新加的Slider控件,打开Attribute Inspector,修改属性值,设置最小值为0,最大值为100,当前值为0.5,并确保勾选上Continuous,如下图:
接着我们放上UISwitch控件,就是很像开关的那种控件,它只有两个状态:on和off,全都放上去效果就是这样的:
2.好了我们开始写代码喽:ViewController.h:
接着是实现 ViewController.m:
3.剩下的就是连接操作和输出口:
将slider控件的value changed事件与sliderChanged方法连接在一起,将swich控件的value changed事件与swichChanged方法连接在一起,当然还要把lable控件和swich控件的输出与ViewController的相应控件接口连接在一起。
最终实现的效果如下面两张图: