`
kzby2003
  • 浏览: 29601 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类
最新评论

文件操作的一些共通方法

 
阅读更多
注释是日文的,懒得翻译了
ファイル: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();
		}
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics