文件操作的一些共通方法

注释是日文的,懒得翻译了 :D
ファイル:file,即文件
再帰:递归
出力ストリームオブジェクト:输出流对象,即outputStream
フォルダ:文件夹
サブフォルダ:子文件夹
複数:多个
パス:path,路径

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileOperation {


/**
* ファイルの圧縮
* @param zipFileName 圧縮されたファイルの名前
* @param inputFileName 圧縮する前のファイル名
* @throws Exception
*/
public void zipFile(String zipFileName, String inputFileName) throws IOException {
ZipOutputStream out = new ZipOutputStream(
new FileOutputStream(zipFileName));
File inputFile = new File(inputFileName);
this.zipFile(out, inputFile, "", true);
out.close();
}


/**
* 再帰フォルダ圧縮ファイル
* @param out 圧縮出力ストリームオブジェクト
* @param file 圧縮されるファイル
* @param base 再帰フォルダ
* @param first フォルダ/ファイルの判断
* @throws IOException
*/
private void zipFile(ZipOutputStream out, File file, String base, boolean first) throws IOException {
if (file.isDirectory()) {
File[] fl = file.listFiles();
if (first) {
first = false;
} else {
base = base + "/";
}
for (int i = 0; i < fl.length; i++) {
this.zipFile(out, fl[i], base + fl[i].getName(), first);
}
} else {
if (first) {
base = file.getName();
}
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(file);
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
in.close();
}
}


/**
* フォルダを全部コピー
* @param oldPath 元ファイルのパス
* @param newPath コピー後のパス
* @throws IOException
*/
public void copyFolder(String oldPath, String newPath) throws IOException {
//フォルダが存在しない場合、新規作成する
new File(newPath).mkdirs();
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +
temp.getName().toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
//サブフォルダの場合
if (temp.isDirectory()) {
this.copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
}


/**
* フォルダを削除
* @param folderPath フォルダパスとフォルダ名
*/
public void delFolder(String folderPath) {
//フォルダにある内容を全部削除
this.delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
//空きフォルダを削除
myFilePath.delete();
}


/**
* フォルダにあるファイルを全部削除
* @param path フォルダパス
*/
public void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
//先ずはフォルダにあるファイルを削除
this.delAllFile(path + "/" + tempList[i]);
//そして空きになったフォルダを削除
this.delFolder(path + "/" + tempList[i]);
}
}
}


/**
* 複数ファイル圧縮機能
* @param zipPath zipファイルのパス
* @param filesNames 指定したフォルダ(複数)
* @throws IOException
*/
public void zipFile(String zipPath, String[] filesNames) throws IOException {
File zipFile = new File(zipPath);
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipFile));
ZipOutputStream out = new ZipOutputStream(bos);
for (int i = 0; i < filesNames.length; i++) {
File file = new File(filesNames[i]);
this.readFile(file, out, file.getName());
}
out.close();
} catch (IOException e) {

}
}


/**
* 再帰してファイルを圧縮
* @param file 指定したファイル
* @param out 圧縮出力ストリームオブジェクト
* @param name 圧縮ファイルのファイル名
* @throws IOException
*/
private void readFile(File file, ZipOutputStream out, String name) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
name = name.length() == 0 ? "" : name + "/";
for (int i = 0; i < files.length; i++) {
File tempFile = files[i];
this.readFile(tempFile, out, name + tempFile.getName());
}
} else {
out.putNextEntry(new ZipEntry(name));
byte[] data = new byte[1024];
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream buffer = new BufferedInputStream(inputStream, 1024);
int count;
while ((count = buffer.read(data, 0, 1024)) != -1) {
out.write(data, 0, count);
}
buffer.close();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值