/**
* 获取当前form文件或者文件夹绝对路径
*
* @author ZengWenFeng
* @date 2017-09-06
* @param form 当前节点
* @param list 树结构所有节点列表(集合)
* @param path 默认输入为空字符串
* @return 返回当前节点对应文件或者文件夹目录字符串(文件为:曾文峰-目录/kkk/zwf13.xls 目录为:曾文峰-目录/kkk
*/
public String getFilePath(FbForm form, List<FbForm> list, String path)
{
if (form == null)
{
return "";
}
String id = form.getId();//id
String fName = form.getName();//excel文件名
String tName = form.getTablename();//数据库表名
String pId = form.getPid();//父节点id
// 父节点为空,则为一级目录或者一级文件
if (pId == null || pId.trim().equals(""))
{
// 数据库表名为空,则是目录,返回目录名:曾文峰-目录
if (tName == null || tName.trim().equals(""))
{
if (path.equals(""))
{
return fName;
}
else
{
return path;
}
}
//数据库表名不为空,则是文件,直接返回excel文件名:曾文锋测试5.XLS
else
{
if (path.equals(""))
{
return fName + ".xls";
}
else
{
return path;
}
}
}
// 父节点不为空
else
{
FbForm parentForm = null;
String id_parentForm = "";//id
String fName_parentForm = "";//excel文件名
String tName_parentForm = "";//数据库表名
String pId_parentForm = "";//父节点id
// 寻找父节点
FbForm tempForm = null;
for (int i = 0; i < list.size(); i++)
{
tempForm = list.get(i);
if (tempForm == null)
{
continue;
}
// 如果找到父节点了
if (pId.equals(tempForm.getId()))
{
parentForm = tempForm;
}
}
if (parentForm == null)
{
return path;
}
id_parentForm = parentForm.getId();//id
fName_parentForm = parentForm.getName();//excel文件名
tName_parentForm = parentForm.getTablename();//数据库表名
pId_parentForm = parentForm.getPid();//父节点id
if (tName == null || tName.trim().equals(""))
{
if (path.equals(""))
{
path = fName_parentForm + "/" + fName;
}
else
{
path = fName_parentForm + "/" + path;
}
}
else
{
if (path.equals(""))
{
path = fName_parentForm + "/" + fName + ".xls";
}
else
{
path = fName_parentForm + "/" + path;
}
}
return getFilePath(parentForm, list, path);
}
}
我X, ZipOutputStream 字符集乱码??明天整。
package zip;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import zip7rar.CompressFile;
/**
*
*
* @author ZengWenFeng
*/
public class ZipCompress
{
/**
* java.util.zip.ZipOutputStream
*
* 乱码
*
* @throws IOException
*/
public static void test01() throws IOException
{
String[] fileNames =
{
"E:/PRJ_J2EE/PrjFlexJava/java_src/zip/config.properties",
"E:/PRJ_J2EE/PrjFlexJava/java_src/zip/中文.properties",
"E:/PRJ_J2EE/PrjFlexJava/java_src/zip/中文.csv"
};
FileOutputStream f = new FileOutputStream("C:/Users/Administrator/Desktop/test.zip");
CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out = new BufferedOutputStream(zos);
zos.setComment("comment");
for (String fileName : fileNames)
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
zos.putNextEntry(new ZipEntry(fileName));
String temp;
while ((temp = in.readLine()) != null)
{
out.write(temp.getBytes("UTF-8"));
}
in.close();
out.flush();
}
out.close();
}
/**
* org.apache.tools.zip.ZipOutputStream
* 没有乱码
*
* @throws IOException
*/
public static void test02() throws IOException
{
File[] fileNames =
{
new File("E:/PRJ_J2EE/PrjFlexJava/java_src/zip/config.properties"),
new File("E:/PRJ_J2EE/PrjFlexJava/java_src/zip/中文.properties"),
new File("E:/PRJ_J2EE/PrjFlexJava/java_src/zip/中文.csv")
};
File zipfile = new File("C:/Users/Administrator/Desktop/test.zip");
CompressFile.zipFiles(fileNames, zipfile);
}
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
test02();
}
}