文件压缩与删除(整理) (转)
java 代码
- /**
- *
- * @param inputFileName
- * 需要压缩的文件路径
- * @param outputFileName
- * 输出的文件名
- * @throws Exception
- */
- public boolean zip(String inputFileName, String outputFileName){
- boolean bNodFoundFile=true;
- ZipOutputStream out = null;
- try {
- out = new ZipOutputStream(new FileOutputStream(
- outputFileName));
- zip(out, new File(inputFileName), "");
- /*注意当压缩文件夹为空时,将抛出异常*/
- out.close();
- } catch (IOException e) {
- bNodFoundFile=false;
- }finally{
- if(out!=null){
- try {
- out.close();
- out=null;
- } catch (IOException e) {
- }
- }
- }
- return bNodFoundFile;
- }
- private void zip(ZipOutputStream out, File f, String base){
- FileInputStream in =null;
- try{
- if (f.isDirectory()) {
- File[] fl = f.listFiles();
- /*此处解决压缩未端数据不正确*/
- if (System.getProperty("os.name").startsWith("Windows")) {
- base = base.length() == 0 ? "" : base + "\";
- out.putNextEntry(new ZipEntry(base));
- } else {
- base = base.length() == 0 ? "" : base + "/";
- out.putNextEntry(new ZipEntry(base));
- }
- int indexSize=fl.length;
- for (int i = 0; i < indexSize; i++) {
- zip(out, fl[i], base + fl[i].getName());
- }
- } else {
- out.putNextEntry(new ZipEntry(base));
- in = new FileInputStream(f);
- int b;
- byte[] buffer = new byte[4096];
- while ((b = in.read(buffer)) != -1) {
- out.write(buffer,0,b);
- }
- }
- }catch(IOException e){
- } finally{
- try {
- if(in!=null){
- in.close();
- in=null;
- }
- } catch (IOException e1) {
- }
- }
- }
- /**
- * 删文件
- * @param deleteFilePath
- * 删除文件路径
- */
- public void deleteFile(final String deleteFilePath){
- File file=new File(deleteFilePath);
- deleteFile(file);
- file.delete();
- }
- /**
- * 实施删除文件
- * @param file
- */
- private void deleteFile(File file){
- if(file.isDirectory()){
- File[] fl = file.listFiles();
- int indexSize=fl.length;
- for(int i=0; i
- deleteFile(fl[i]);
- }
- /*此处删除目录*/
- file.delete();
- }else{
- /*此处删除文件*/
- file.delete();
- }
- }
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12467/viewspace-148291/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/12467/viewspace-148291/