文件遍历
2007-12-06 14:50
183 查看
public String zipFiles(String[] fnames,HttpServletRequest request) {
ArrayList fileNames = new ArrayList();
ArrayList files = new ArrayList(); // 存放文件对象
String filePath = "/download/";
String serverPath = request.getRealPath("/");
String getfn = getFileName("zip");
StringBuffer sbf = new StringBuffer();
sbf.append(serverPath);
sbf.append(filePath);
sbf.append(getfn);
sbf.append(".rar");
String fullPaht = serverPath+filePath+getfn+".zip";
try {
FileOutputStream fileOut = new FileOutputStream(fullPaht);
ZipOutputStream outputStream = new ZipOutputStream(fileOut);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
File rootFile = null;
for (int s = 0; s < fnames.length; s++) {
rootFile = new File(fnames[s]);
listFile(rootFile, fileNames, files);
}
for (int loop = 0; loop < files.size(); loop++) {
FileInputStream fileIn = new FileInputStream((File) files.get(loop));
BufferedInputStream bisOne = new BufferedInputStream(fileIn);
outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop)));
int value = -1;
while ((value = bisOne.read()) != -1) {
bos.write(value);
}
bos.flush();
bisOne.close();
// byte[] buffer = new byte[1024];
// while (fileIn.read(buffer) != -1) {
// outputStream.write(buffer);
// }
// fileIn.close();
// outputStream.closeEntry();
}
outputStream.close();
// fileOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fullPaht;
}
static void listFile(File parentFile, List nameList, List fileList) {
if (parentFile.isDirectory()) {
File[] files = parentFile.listFiles();
for (int loop = 0; loop < files.length; loop++) {
listFile(files[loop], nameList, fileList);
}
} else {
fileList.add(parentFile);
nameList.add(parentFile.getName());
}
}
public static String s_cl(String s){
s = s.trim();
if(s==null || s.equals("") || s.equals("null")){
s = "";
}
return s;
}
ArrayList fileNames = new ArrayList();
ArrayList files = new ArrayList(); // 存放文件对象
String filePath = "/download/";
String serverPath = request.getRealPath("/");
String getfn = getFileName("zip");
StringBuffer sbf = new StringBuffer();
sbf.append(serverPath);
sbf.append(filePath);
sbf.append(getfn);
sbf.append(".rar");
String fullPaht = serverPath+filePath+getfn+".zip";
try {
FileOutputStream fileOut = new FileOutputStream(fullPaht);
ZipOutputStream outputStream = new ZipOutputStream(fileOut);
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
File rootFile = null;
for (int s = 0; s < fnames.length; s++) {
rootFile = new File(fnames[s]);
listFile(rootFile, fileNames, files);
}
for (int loop = 0; loop < files.size(); loop++) {
FileInputStream fileIn = new FileInputStream((File) files.get(loop));
BufferedInputStream bisOne = new BufferedInputStream(fileIn);
outputStream.putNextEntry(new ZipEntry((String) fileNames.get(loop)));
int value = -1;
while ((value = bisOne.read()) != -1) {
bos.write(value);
}
bos.flush();
bisOne.close();
// byte[] buffer = new byte[1024];
// while (fileIn.read(buffer) != -1) {
// outputStream.write(buffer);
// }
// fileIn.close();
// outputStream.closeEntry();
}
outputStream.close();
// fileOut.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return fullPaht;
}
static void listFile(File parentFile, List nameList, List fileList) {
if (parentFile.isDirectory()) {
File[] files = parentFile.listFiles();
for (int loop = 0; loop < files.length; loop++) {
listFile(files[loop], nameList, fileList);
}
} else {
fileList.add(parentFile);
nameList.add(parentFile.getName());
}
}
public static String s_cl(String s){
s = s.trim();
if(s==null || s.equals("") || s.equals("null")){
s = "";
}
return s;
}
相关文章推荐
- 文件操作及字符串操作(二):遍历文件夹下的所有文件
- python遍历目录和文件
- Python os.walk文件遍历
- python天天进步--文件操作之遍历目录
- JS如何遍历一个文件夹下的所有文件与目录
- Node遍历目录中的文件
- php实现目录及目录文件下的遍历
- 如何遍历所有文件夹,获取某一类型文件
- 用一个函数实现遍历一个文件夹下的所以文件和子文件夹
- 遍历一个文件夹,删除指定数量多余时间最久的文件
- anjularjs遍历json文件
- 遍历指定文件夹下所有的xml文件并动态生成HTML页面!
- 递归遍历路径下所有文件算法
- Linux_shell脚本_遍历文件夹下所有文件
- CMD下一个命令遍历目录删除相同垃圾文件
- 第16课 Python循环体for遍历文件和元组
- va_list va_start cfindfile遍历文件
- C#遍历指定文件夹中的所有文件
- 遍历读取apk安装后压缩包内的文件,判断软件是否安装成功
- 遍历某目录中所有文件