当前位置: 编程技术>移动开发
本页文章导读:
▪本土监听的使用 本地监听的使用
//为tempURL添加一个监听。 [self addObserver:self forKeyPath:@"tempURL" options:NSKeyValueObservingOptionNew context:nil];-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *).........
▪ java 用haozip解压正常,用winrar解压报错:这个压缩文件格式未知或者数据异常,求援助 java 用haozip解压正常,用winrar解压报错:这个压缩文件格式未知或者数据错误,求援助
package com.cmsz.resource.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Fi.........
▪ 【Service & BroadcastReceiver相干】 【Service & BroadcastReceiver相关】
android开发中如何实现开机自启动 http://gundumw100.iteye.com/blog/906188监听应用程序安装和卸载 http://zhangkun716717-126-com.iteye.com/blog/1192479使用service定期执行一个服.........
[1]本土监听的使用
来源: 互联网 发布时间: 2014-02-18
本地监听的使用
//为tempURL添加一个监听。
[self addObserver:self forKeyPath:@"tempURL" options:NSKeyValueObservingOptionNew context:nil];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
[self.liShiDict setObject:self.tempURL forKey:self.tempTitle];
}
上面的操作就 实现了: 当属性 tempURL 变化的时候,就促发下面的方法。。
[2] java 用haozip解压正常,用winrar解压报错:这个压缩文件格式未知或者数据异常,求援助
来源: 互联网 发布时间: 2014-02-18
java 用haozip解压正常,用winrar解压报错:这个压缩文件格式未知或者数据错误,求援助
package com.cmsz.resource.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
*
* @author pengming01
*
*/
public class FilesManager {
/**
* 根据目录打包
*
* @param SourcePath
* 打包目录
* @param zipFilePath
* 目标压缩文件路径
*/
public String zip(String zipFilePath, String inputFolderName)
throws Exception {
String zipFileName = zipFilePath; // 打包后文件名字
File zipFile = new File(inputFolderName);
FileOutputStream fileOut = new FileOutputStream(zipFileName);
ZipOutputStream out = new ZipOutputStream(fileOut);
zip(out, zipFile, "");
out.close();
fileOut.close();
return zipFilePath;
}
public void zip(ZipOutputStream out, File inputFolder, String base)
throws Exception {
if (inputFolder.isDirectory()) {
File[] fl = inputFolder.listFiles();
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
} else {
out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
FileInputStream in = new FileInputStream(inputFolder);
int b;
// System.out.println(base);
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}
/**
* 根据目录打包
*
* @param SourcePath
* 打包目录
* @param zipFilePath
* 目标压缩文件路径
*/
public void folderZip(String SourcePath, String zipFilePath) {
File zipFile = new File(SourcePath);
int len = zipFile.listFiles().length;
String[] filenames = new String[len];
byte[] buf = new byte[1024];
try {
File[] files = zipFile.listFiles();
for (int i = 0; i < len; i++) {
filenames[i] = zipFile.getPath() + File.separator
+ files[i].getName();
}
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
zipFilePath));
for (int i = 0; i < filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);
out.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
public static void zipFile(String baseDirName, String[] fileNames,
String targetFileName) throws IOException {
/**
* 由这个 "压缩的根目录" 文件名路径得到一个 File 对象 并判断这个 File 对象表示的文件是否存在! 是否是一个文件夹!.....
*/
File baseDir = new File(baseDirName);
if (!baseDir.exists() || (!baseDir.isDirectory())) {
System.out.println("压缩失败! 根目录不存在: " + baseDirName);
return;
}
// 得到这个 "压缩的根目录" 的路径.........
String baseDirPath = baseDir.getAbsolutePath();
/**
* 由这个 "目标 ZIP 文件" 文件名得到一个 压缩对象 ZipOutputStream
*/
File targetFile = new File(targetFileName);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
targetFile));
// "*" 表示压缩包括根目录 baseDirName 在内的全部文件 到 targetFileName文件下
if (fileNames.equals("*")) {
FilesManager.dirToZip(baseDirPath, baseDir, out);
} else {
File[] files = new File[fileNames.length];
for (int i = 0; i < files.length; i++) {
// 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
files[i] = new File(baseDir, fileNames[i]);
}
if (files[0].isFile()) {
// 调用本类的一个静态方法 压缩一个文件
// CompressUtil.fileToZip(baseDirPath, file, out);
FilesManager.filesToZip(baseDirPath, files, out);
}
}
out.close();
System.out.println("压缩成功! 目标文件名为: " + targetFileName);
}
/**
*
* *************************************************多个文件目录压缩到Zip 输出流
* *************************************************
*/
public static void filesToZip(String baseDirPath, File[] files,
ZipOutputStream out) throws IOException {
// 遍历所有的文件 一个一个地压缩
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
// 调用本类的一个静态方法 压缩一个文件
FilesManager.fileToZip(baseDirPath, file, out);
} else {
/*
* 这是一个文件夹 所以要再次得到它下面的所有的文件
* 这里是自己调用自己..............递归..........
*/
FilesManager.dirToZip(baseDirPath, file, out);
}
}
}
public static void dirToZip(String baseDirPath, File dir,
ZipOutputStream out) throws IOException {
// 得到一个文件列表 (本目录下的所有文件对象集合)
File[] files = dir.listFiles();
/**
* 要是这个文件集合数组的长度为 0 , 也就证明了这是一个空的文件夹
*
* 虽然没有再循环遍历它的必要,但是也要把这个空文件夹也压缩到目标文件中去
*/
if (files.length == 0) {
// 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
String zipFileName = getEntryName(baseDirPath, dir);
System.out.println("zipFileName--------------------------"+zipFileName);
ZipEntry entry = new ZipEntry(zipFileName);
System.out.println("entry.getName()-============="+entry.getName());
out.putNextEntry(entry);
out.closeEntry();
} else {
// 遍历所有的文件 一个一个地压缩
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile()) {
// 调用本类的一个静态方法 压缩一个文件
FilesManager.fileToZip(baseDirPath, file, out);
} else {
/*
* 这是一个文件夹 所以要再次得到它下面的所有的文件
* 这里是自己调用自己..............递归..........
*/
FilesManager.dirToZip(baseDirPath, file, out);
}
}
}
}
/**
*
* ************************************************* 获取待压缩文件在 ZIP 文件中的 entry
* 的名字! 即相对于根目录的相对路径名 *************************************************
*
*/
public static String getEntryName(String baseDirPath, File file) {
/**
* 改变 baseDirPath 的形式 把 "C:/temp" 变成 "C:/temp/"
*/
if (!baseDirPath.endsWith(File.separator)) {
baseDirPath += File.separator;
System.out.println(baseDirPath);
}
String filePath = file.getAbsolutePath();
/**
* 测试此抽象路径名表示的文件是否是一个目录。 要是这个文件对象是一个目录 则也要变成 后面带 "/"
*
* 这个文件对象类似于 "C:/temp/人体写真/1.jpg"
*
* 要是这个文件是一个文件夹 则也要变成 后面带 "/" 因为你要是不这样做,它也会被压缩到目标文件中 但是却不能正解显示
* 也就是说操作系统不能正确识别它的文件类型(是文件还是文件夹)
*/
if (file.isDirectory()) {
filePath += "\\";
}
int index = filePath.indexOf(baseDirPath);
return filePath.substring(index + baseDirPath.length());
}
public static void fileToZip(String baseDirPath, File file,
ZipOutputStream out) throws IOException {
//
FileInputStream in = null;
ZipEntry entry = null;
// 创建复制缓冲区 1024*4 = 4K
byte[] buffer = new byte[1024 * 4];
int bytes_read = 0;
out.setEncoding("GBK");
if (file.isFile()) {
in = new FileInputStream(file);
// 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例
String zipFileName = getEntryName(baseDirPath, file);
System.out.println("baseDirPath========="+baseDirPath);
System.out.println("===================="+zipFileName);
entry = new ZipEntry(zipFileName);
// "压缩文件" 对象加入 "要压缩的文件" 对象
out.putNextEntry(entry);
// 现在是把 "要压缩的文件" 对象中的内容写入到 "压缩文件" 对象
while ((bytes_read = in.read(buffer)) != -1) {
out.write(buffer, 0, bytes_read);
}
out.closeEntry();
in.close();
System.out.println("添加文件" + file.getAbsolutePath()
+ "被添加到 ZIP 文件中!");
}
}
public static void main(String[] args) {
FilesManager zt = new FilesManager();
File[] fileList = new File[3];
File file1 = new File("D:\\新业务集成测试系统\\业务集成测试跟踪及注意事项.xlsx");
File file2 = new File("D:\\新业务集成测试系统\\测试2");
File file3 = new File("D:\\新业务集成测试系统\\资源共享验收测试\\资源共享验收测试\\测试用例_自动化网厅账单查询_20130124.xlsx");
File zipFile = new File("D:\\测试.zip");
fileList[1]=file2;
fileList[2]=file3;
fileList[0]=file1;
ZipOutputStream zipOut;
try {
zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
try {
FilesManager.filesToZip("D:\\新业务集成测试系统", fileList, zipOut);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fileList.add(file2);
//fileList.add(file3);
}
}
[3] 【Service & BroadcastReceiver相干】
来源: 互联网 发布时间: 2014-02-18
【Service & BroadcastReceiver相关】
android开发中如何实现开机自启动
http://gundumw100.iteye.com/blog/906188
监听应用程序安装和卸载
http://zhangkun716717-126-com.iteye.com/blog/1192479
使用service定期执行一个服务
http://gundumw100.iteye.com/blog/896880
利用BroadcastReceiver监听短信
http://gundumw100.iteye.com/blog/875951
使用Service和BroadcastReceiver实时监听网络状态
http://gundumw100.iteye.com/blog/1732865
android开发中如何实现开机自启动
http://gundumw100.iteye.com/blog/906188
监听应用程序安装和卸载
http://zhangkun716717-126-com.iteye.com/blog/1192479
使用service定期执行一个服务
http://gundumw100.iteye.com/blog/896880
利用BroadcastReceiver监听短信
http://gundumw100.iteye.com/blog/875951
使用Service和BroadcastReceiver实时监听网络状态
http://gundumw100.iteye.com/blog/1732865
最新技术文章: