FIle工具
读取ZIP压缩包
public class FileUtils{
public static long getSize(InputStream is){
if(is == null){
return 0l;
}
if(is.markSupported()){
return getSizeMark(is);
}
return getSizeMark;
}
private static long getSizeNoMark(InputStream is){
// 缓冲区大小1k
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
long size = 0l;
try {
bis = new BufferedInputStream(is);
int read = bis.read(buff);
size += read;
// 通过while循环写入到指定了的文本流中
while(read != -1){
read = bis.read(buff);
size += read;
}
size++;// 抵最后一次的-1
} catch(IOException e) {
e.printStackTrace();
}
return size;
}
private static long getSizeMark(InputStream is){
// 缓冲区大小1k
byte[] buff = new byte[1024];
BufferedInputStream bis = null;
long size = 0l;
try {
is.mark(Integer.MAX_VALUE);
bis = new BufferedInputStream(is);
int read = bis.read(buff);
size += read;
// 通过while循环写入到指定了的文本流中
while(read != -1){
read = bis.read(buff);
size += read;
}
size++;// 抵最后一次的-1
is.reset();
} catch(IOException e) {
e.printStackTrace();
}
return size;
}
public static void writeTextToFile(String text,File file,boolean append){
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
if(!file.exists()){
file.createNewFile();
}
fos = new FileOutputStream(file,append);
osw = new OutputStreamWriter(fos,"UTF-8");
bw = new BufferedWriter(osw);
bw.write(text.trim());
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(bw != null){
bw.flush();
bw.close();
}
if(osw != null){
osw.close();
}
if(fos != null){
fos.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
public static boolean compress(File zip,File[] sources,String dir){
ZipOutputStream zos = null;
FileOutputStream fos = null;
try{
if(!zip.getParentFile().exists()){
zip.getParentFile().mkdirs();
}
if (!zip.exists()){
zip.createNewFile();
}
fos = new FileOutputStream(zip);
zos = new ZipOutputStream(zip);
zip(sources,dir,zos);
} catch (IOException e) {// 文件压缩异常
e.printStackTrace();
} finally {
try {
if (zos != null){
zos.close();
}
if (fos != null){
fos.close();
}
} catch (IOException e){// 文件压缩异常-关流
e.printStackTrace();
}
}
}
return true;
}
private static void zip(File[] sources,String dir,ZipOutputStream zos){
for (File source : sources){
if (source.isDirectory()){
compressDir(source,dir,zos);
} else {
doZip(zos,source,dir);
}
}
}
private static void compressDir(File source,String dir,ZipOutputStream zos){
File[] files = source.listFiles();
if (files != null && files.length > 0){
zip(files,dir + File.separator + source.getName(),zos);
}
}
private static void doZip(ZipOutputStream zos,File source,String dir){
String entryName = null;
if (dir != null && dir != ""){
entryName = dir + "/" + source.getName();
} else {
entryName = source.getName();
}
ZipEntry entry = new ZipEntry(entryName);
zos.putNextEntry(entry);
FileInputStream fis = null;
try {
int len = 0;
byte[] buffer = new byte[1024];
fis = new FileInputStream(source);
while((len = fis.read(buffer)) > 0){
zos.write(buffer,0,len);
zos.flush();
}
} finally {
zos.closeEntry();
if (fis != null)
fis.close();
}
}
public static void main(String[] args){
String zipPath = "D:\\myzip\\test.zip";
String path = "D:\\myzip";
String dir = "";
try {
compress(new File(zipPath),(new File(path)).listFiles(),dir);
} catch(Exception e) {
e.printStackTrace();
}
}
}