资料工具

文件工具


	/**
	 * 将指定路径的文件转成二进制流返回出来
	 * @param zipfile 文件路径
	 * @return
	 */
	public static byte[] fileToByte(String path){
		File zipfile = new File(path);
		FileInputStream fis = null;
		byte[] b = null;
		try {
			fis = new FileInputStream(zipfile);
			b = new byte[fis.available()];
			fis.read(b);
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		return b;
	}

/**
	 * 将多个二进制文件压缩到知道目录
	 * @param fileList 文件集合
	 * @param zipfile 压缩目录
	 */
	public static void ZipFiles(List<byte[]> fileList,String path) {
		try {
			OutputStream fileOutput = new FileOutputStream(path);
			ZipOutputStream out = new ZipOutputStream(fileOutput);
			out.setMethod(ZipOutputStream.DEFLATED);
			for (int i = 0; i < fileList.size(); i++) {
				byte[] file = fileList.get(i);
				out.putNextEntry(new ZipEntry(i+".jpg"));
				out.write(file,0,file.length);
				out.closeEntry();
			}
			out.flush();
			out.close();
			fileOutput.close();
		} catch (ZipException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除指定路径的单个文件
	 * @param path 文件目录
	 */
	public static void deleteFile(String path){
		File file = new File(path);
		if(file.exists()){
			if(file.isFile()){
				file.delete();
			}
		}
	}