当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 1. [代码]DirTraversal.javapackage com.once;import java.io.File;import java.util.ArrayList;import java.util.LinkedList;/*** 文件夹遍历* @author once**/public cl

 1. [代码]DirTraversal.java

package com.once;

import java.io.File;

import java.util.ArrayList;

import java.util.LinkedList;

/**

* 文件夹遍历

* @author once

*

*/

public class DirTraversal {

//no recursion

public static LinkedList listLinkedFiles(String strPath) {

LinkedList list = new LinkedList();

File dir = new File(strPath);

File file[] = dir.listFiles();

for (int i = 0; i < file.length; i++) {

if (file[i].isDirectory())

list.add(file[i]);

else

System.out.println(file[i].getAbsolutePath());

}

File tmp;

while (!list.isEmpty()) {

tmp = (File) list.removeFirst();

if (tmp.isDirectory()) {

file = tmp.listFiles();

if (file == null)

continue;

for (int i = 0; i < file.length; i++) {

if (file[i].isDirectory())

list.add(file[i]);

else

System.out.println(file[i].getAbsolutePath());

}

} else {

System.out.println(tmp.getAbsolutePath());

}

}

return list;

}

//recursion

public static ArrayList listFiles(String strPath) {

return refreshFileList(strPath);

}

public static ArrayList refreshFileList(String strPath) {

ArrayList filelist = new ArrayList();

File dir = new File(strPath);

File[] files = dir.listFiles();

if (files == null)

return null;

for (int i = 0; i < files.length; i++) {

if (files[i].isDirectory()) {

refreshFileList(files[i].getAbsolutePath());

} else {

if(files[i].getName().toLowerCase().endsWith("zip"))

filelist.add(files[i]);

}

}

return filelist;

}

}

2. [代码]ZipUtils.java

package com.once;

import java.io.*;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Enumeration;

import java.util.zip.ZipEntry;

import java.util.zip.ZipException;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

/**

* Java utils 实现的Zip工具

*

* @author once

*/

public class ZipUtils {

private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte

/**

* 批量压缩文件(夹)

*

* @param resFileList 要压缩的文件(夹)列表

* @param zipFile 生成的压缩文件

* @throws IOException 当压缩过程出错时抛出

*/

public static void zipFiles(Collection resFileList, File zipFile) throws IOException {

ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(

zipFile), BUFF_SIZE));

for (File resFile : resFileList) {

zipFile(resFile, zipout, "");

}

zipout.close();

}

/**

* 批量压缩文件(夹)

*

* @param resFileList 要压缩的文件(夹)列表

* @param zipFile 生成的压缩文件

* @param comment 压缩文件的注释

* @throws IOException 当压缩过程出错时抛出

*/

public static void zipFiles(Collection resFileList, File zipFile, String comment)

throws IOException {

ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(

zipFile), BUFF_SIZE));

for (File resFile : resFileList) {

zipFile(resFile, zipout, "");

}

zipout.setComment(comment);

zipout.close();

}

/**

* 解压缩一个文件

*

* @param zipFile 压缩文件

* @param folderPath 解压缩的目标目录

* @throws IOException 当解压缩过程出错时抛出

*/

public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {

File desDir = new File(folderPath);

if (!desDir.exists()) {

desDir.mkdirs();

}

ZipFile zf = new ZipFile(zipFile);

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {

ZipEntry entry = ((ZipEntry)entries.nextElement());

InputStream in = zf.getInputStream(entry);

String str = folderPath + File.separator + entry.getName();

str = new String(str.getBytes("8859_1"), "GB2312");

File desFile = new File(str);

if (!desFile.exists()) {

File fileParentDir = desFile.getParentFile();

if (!fileParentDir.exists()) {

fileParentDir.mkdirs();

}

desFile.createNewFile();

}

OutputStream out = new FileOutputStream(desFile);

byte buffer[] = new byte[BUFF_SIZE];

int realLength;

while ((realLength = in.read(buffer)) > 0) {[!--empirenews.page--]

out.write(buffer, 0, realLength);

}

in.close();

out.close();

}

}

/**

* 解压文件名包含传入文字的文件

*

* @param zipFile 压缩文件

* @param folderPath 目标文件夹

* @param nameContains 传入的文件匹配名

* @throws ZipException 压缩格式有误时抛出

* @throws IOException IO错误时抛出

*/

public static ArrayList upZipSelectedFile(File zipFile, String folderPath,

String nameContains) throws ZipException, IOException {

ArrayList fileList = new ArrayList();

File desDir = new File(folderPath);

if (!desDir.exists()) {

desDir.mkdir();

}

ZipFile zf = new ZipFile(zipFile);

for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {

ZipEntry entry = ((ZipEntry)entries.nextElement());

if (entry.getName().contains(nameContains)) {

InputStream in = zf.getInputStream(entry);

String str = folderPath + File.separator + entry.getName();

str = new String(str.getBytes("8859_1"), "GB2312");

// str.getBytes("GB2312"),"8859_1" 输出

// str.getBytes("8859_1"),"GB2312" 输入

File desFile = new File(str);

if (!desFile.exists()) {

File fileParentDir = desFile.getParentFile();

if (!fileParentDir.exists()) {

fileParentDir.mkdirs();

}

desFile.createNewFile();

}

OutputStream out = new FileOutputStream(desFile);

byte buffer[] = new byte[BUFF_SIZE];

int realLength;

while ((realLength = in.read(buffer)) > 0) {

out.write(buffer, 0, realLength);

}

in.close();

out.close();

fileList.add(desFile);

}

}

return fileList;

}

/**

* 获得压缩文件内文件列表

*

* @param zipFile 压缩文件

* @return 压缩文件内文件名称

* @throws ZipException 压缩文件格式有误时抛出

* @throws IOException 当解压缩过程出错时抛出

*/

public static ArrayList getEntriesNames(File zipFile) throws ZipException, IOException {

ArrayList entryNames = new ArrayList();

Enumeration entries = getEntriesEnumeration(zipFile);

while (entries.hasMoreElements()) {

ZipEntry entry = ((ZipEntry)entries.nextElement());

entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));

}

return entryNames;

}

/**

* 获得压缩文件内压缩文件对象以取得其属性

*

* @param zipFile 压缩文件

* @return 返回一个压缩文件列表

* @throws ZipException 压缩文件格式有误时抛出

* @throws IOException IO操作有误时抛出

*/

public static Enumeration getEntriesEnumeration(File zipFile) throws ZipException,

IOException {

ZipFile zf = new ZipFile(zipFile);

return zf.entries();

}

/**

* 取得压缩文件对象的注释

*

* @param entry 压缩文件对象

* @return 压缩文件对象的注释

* @throws UnsupportedEncodingException

*/

public static String getEntryComment(ZipEntry entry) throws UnsupportedEncodingException {

return new String(entry.getComment().getBytes("GB2312"), "8859_1");

}

/**

* 取得压缩文件对象的名称

*

* @param entry 压缩文件对象

* @return 压缩文件对象的名称

* @throws UnsupportedEncodingException

*/

public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {

return new String(entry.getName().getBytes("GB2312"), "8859_1");

}

/**

* 压缩文件

*

* @param resFile 需要压缩的文件(夹)

* @param zipout 压缩的目的文件

* @param rootpath 压缩的文件路径

* @throws FileNotFoundException 找不到文件时抛出

* @throws IOException 当压缩过程出错时抛出

*/

private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)

throws FileNotFoundException, IOException {

rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator)

+ resFile.getName();

rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");

if (resFile.isDirectory()) {

File[] fileList = resFile.listFiles();

for (File file : fileList) {

zipFile(file, zipout, rootpath);

}

} else {

byte buffer[] = new byte[BUFF_SIZE];

BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile),

BUFF_SIZE);

zipout.putNextEntry(new ZipEntry(rootpath));

int realLength;

[!--empirenews.page--]

while ((realLength = in.read(buffer)) != -1) {

zipout.write(buffer, 0, realLength);

}

in.close();

zipout.flush();

zipout.closeEntry();

}

}

}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

上海2025年9月5日 /美通社/ -- 由纽伦堡会展(上海)有限公司举办的上海国际嵌入式会议将于 2025 年 10 月 16-17 日在上海世博展览馆举办。 此次会议将由三个版块组成:嵌入式技术会议、汽...

关键字: 嵌入式 CE CHINA EMBEDDED

开创中国文旅产业AI深度应用新样本 北京2025年8月22日 /美通社/ -- 以下为来自亿欧的报道: 8月22日,桂林旅游股份有限公司旗下银子岩景区联合合作伙伴正式发布全球首款AI伴游财神玩具 —— "五...

关键字: AI IP 数字化 硬件

马来西亚吉隆坡2025年8月14日 /美通社/ -- 全球云通信平台Infobip今日发布最新报告《AI优势:领先品牌如何在全天候客户世界中蓬勃发展》(The AI Advantage: How Leading...

关键字: 人工智能 IP 智能体 IDC

 - CAS SciFinder集成变革性的新型科学智能AI功能,以提高研发效率和促进创新 开创性的解决方案能够更快速地为科学家提供可操作的答案,从而加速科学发现 俄亥俄...

关键字: 集成 AI FINDER IP

其他电脑(比如安卓手机/平板电脑)的屏幕坏了,你可能想在安排维修之前紧急访问一些东西。你可以使用android的USB OTG功能(是的,几乎每个android都支持这个功能,你可以将鼠标和键盘连接到它)。

关键字: USB 鼠标 Android 树莓派

RISC-V生态的快速发展源于业界对这一开放指令集体系结构的共同信念,然而其发展并非一帆风顺。企业在推广RISC-V时面临诸多现实问题,包括来自客户客户的质疑、与Arm的差异化价值、软件移植的难度等等。但这些挑战正在逐步...

关键字: RISC-V CPU 香山 昆明湖 IP AI

TCP/IP(Transmission Control Protocol/Internet Protocol,传输控制协议/网际协议)是指能够在多个不同网络间实现信息传输的协议簇。TCP/IP协议不仅仅指的是TCP 和I...

关键字: TCP IP

北京市中国国际展览中心(顺义馆)先进制造馆 W2 展馆 D07 展位 作为链博会先进制造链专业委员会主席单位,发挥"链主"引领和赋能作用 集中呈现西门子覆盖产品...

关键字: 西门子 BSP 数字化 CE

北京 2025年7月9日 /美通社/ -- 在人工智能行业竞争日益白热化的当下,思必驰科技股份有限公司(下称"思必驰")重启科创板 IPO的消息一出,便引发了广泛关注。这家成立于2007年的企业,堪...

关键字: 思必驰 IP AI 模型

广州 2025年7月4日 /美通社/ -- 日前,在德国慕尼黑机器人及自动化技术展览会(Automatica)期间,国际独立第三方检测、检验和认证机构德国莱茵TÜ...

关键字: 自动化 CE 工业机器 指令
关闭