zoukankan      html  css  js  c++  java
  • 文件移动,压缩

    package cn.com.util;
    
    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.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.springframework.stereotype.Component;
    
    import com.zhuozhengsoft.pageoffice.PDFCtrl;
    import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
    
    import cn.com.szhtkj.controller.xtgl.BaseController;
    
    @Component
    public class CommonUtils  extends BaseController{
    	
    	/**
    	 * 创建文件夹
    	 * @param filePath
    	 * @return
    	 */
    	public static void fileMake(String filePath){
    
    		File fileMake = new File(filePath);
    		if (!fileMake.exists()) {
    			fileMake.mkdirs();
    		}
    	}
    	
    	/**
    	 *  主会计科目
    	 * @param kjkm[] 选择所有的科目
    	 * @return List<String> 主科目
    	 */
    	public static List<String> getKjkm(String kjkm[]){
    
    		//会计科目
    		Arrays.sort(kjkm);
    		List<String> kjkmNewList = new ArrayList<String>(); 
    		for(int i = 0; i < kjkm.length; i++) {
    			String kjkmTemp = kjkm[i].toString();
    			boolean flg = true;
    			for(int j = 0; j < kjkm.length; j++) {
    				if(kjkmTemp.indexOf(kjkm[j]) >= 0 && kjkmTemp.length() != kjkm[j].length()) {
    					flg = false;
    					break;
    				}
    			}
    			
    			if(flg) {
    				kjkmNewList.add(kjkm[i]);
    			}
    		}
    		
    		return kjkmNewList;
    	}
    
    	/**
    	 * 计算新的年月
    	 * @param year_month 年月
    	 * @param al_month 差值
    	 * @return 新的年月
    	 */
    	public static String getRelativeYM(String year_month,int al_month){
    
    		//年
    		long year = Long.parseLong(year_month.substring(0, 4));
    		//月
    		long month = Long.parseLong(year_month.substring(4, 6));
    		//总月数
    		long month_all = year * Constants.LONG_TWELVE + month + Constants.INT_ONE;
    		
    		//加上差值
    		month_all += al_month;
    		//新的年份
    		year = Math.abs(month_all / Constants.LONG_TWELVE);
    		//新的月份
    		month = month_all - year * Constants.LONG_TWELVE;
    		if(month == Constants.INT_ZERO) {
    			month = Constants.LONG_TWELVE;
    			year--;
    		}
    		
    		//返回年月
    		return Long.toString(year) + String.format("%02d",month);
    		
    	}
    	
    	/**
         * 压缩文件
         * @param srcFilePath 压缩源路径
         * @param destFilePath 压缩目的路径
         */
        public static void compress(String srcFilePath, String destFilePath) {
            //
            File src = new File(srcFilePath);
            if (!src.exists()) {
                throw new RuntimeException(srcFilePath + "不存在");
            }
            File zipFile = new File(destFilePath);
    
            try {
                FileOutputStream fos = new FileOutputStream(zipFile);
                ZipOutputStream zos = new ZipOutputStream(fos);
                String baseDir = "";
                compressbyType(src, zos, baseDir);
                zos.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
     
            }
        }
        
        /**
         * 按照原路径的类型就行压缩,文件路径直接把文件压缩
         * @param src
         * @param zos
         * @param baseDir
         */
        private static void compressbyType(File src, ZipOutputStream zos,String baseDir) {
     
            if (!src.exists())
                return;
            System.out.println("压缩路径" + baseDir + src.getName());
            //判断文件是否是文件,如果是文件调用compressFile方法,如果是路径,则调用compressDir方法;
            if (src.isFile()) {
                //src是文件,调用此方法
                compressFile(src, zos, baseDir);
            } else if (src.isDirectory()) {
                //src是文件夹,调用此方法
                compressDir(src, zos, baseDir);
            }
    	}
          
         /**
          * 压缩文件
          * @param file
          * @param zos
          * @param baseDir
          */
         private static void compressFile(File file, ZipOutputStream zos,String baseDir) {
        	 if (!file.exists())
                 return;
             try {
                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                 ZipEntry entry = new ZipEntry(baseDir + file.getName());
                 zos.putNextEntry(entry);
                 int count;
                 byte[] buf = new byte[1024];
                 while ((count = bis.read(buf)) != -1) {
                     zos.write(buf, 0, count);
                 }
                 bis.close();
             } catch (Exception e) {
               // TODO: handle exception
             }
    	}
             
    	/**
    	 * 压缩文件夹
    	 * @param dir
         * @param zos
         * @param baseDir
    	 */
    	private static void compressDir(File dir, ZipOutputStream zos,String baseDir) {
    		if (!dir.exists())
                return;
            File[] files = dir.listFiles();
            if(files.length == 0){
                try {
                    zos.putNextEntry(new ZipEntry(baseDir + dir.getName()+File.separator));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            for (File file : files) {
                compressbyType(file, zos, baseDir + dir.getName() + File.separator);
            }
    	}
    	
    	/**
    	 * 复制文件夹 
         */
    	public static void copyDirectiory(String sourceDir, String targetDir) throws IOException {
    		// 新建目标目录 
            (new File(targetDir)).mkdirs();
            // 获取源文件夹当前下的文件或目录 
            File[] file = (new File(sourceDir)).listFiles();
            for (int i = 0; i < file.length; i++) {
                if (file[i].isFile()) {
                	// 源文件 
                    File sourceFile = file[i];
                    // 目标文件 
                    File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
                    copyFile(sourceFile,targetFile);
                }
                if (file[i].isDirectory()) {
                    // 准备复制的源文件夹
                    String dir1 = sourceDir + "/" + file[i].getName();
                    // 准备复制的目标文件夹
                    String dir2 = targetDir + "/" + file[i].getName();
                    copyDirectiory(dir1, dir2);
                }
            }
    	}
    	
    	/**
    	 * 复制文件
         */
    	public static void copyFile(File sourceFile,File targetFile) throws IOException{
    		
    		// 新建文件输入流并对它进行缓冲 
            FileInputStream input = new FileInputStream(sourceFile);
            BufferedInputStream inBuff=new BufferedInputStream(input);
      
            // 新建文件输出流并对它进行缓冲 
            FileOutputStream output = new FileOutputStream(targetFile);
            BufferedOutputStream outBuff=new BufferedOutputStream(output);
            
            // 缓冲数组 
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len =inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流 
            outBuff.flush();
            
            //关闭流 
            inBuff.close();
            outBuff.close();
            output.close();
            input.close();
    	}
    	
    	/**
    	 * 打开文件(不含pdf)
         */
    	public static PageOfficeCtrl openFile(HttpServletRequest request) {
    		PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
    		poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");//设置授权程序servlet
    	    poCtrl.setTitlebar(false); //隐藏标题栏
    	    poCtrl.setMenubar(false); //隐藏菜单栏
    	    poCtrl.setOfficeToolbars(false);//隐藏Office工具条
    	    poCtrl.setCustomToolbar(false);
    		return poCtrl;
    	}
    	
    	/**
    	 * 打开pdf文件
         */
    	public static PDFCtrl openPdfFile(HttpServletRequest request) {
    		PDFCtrl pdfCtrl1 = new PDFCtrl(request);
            pdfCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //此行必须
            // Create custom toolbar
            /*pdfCtrl1.addCustomToolButton("打印", "PrintFile()", 6);
            pdfCtrl1.addCustomToolButton("隐藏/显示书签", "SetBookmarks()", 0);
            pdfCtrl1.addCustomToolButton("-", "", 0);
            pdfCtrl1.addCustomToolButton("实际大小", "SetPageReal()", 16);
            pdfCtrl1.addCustomToolButton("适合页面", "SetPageFit()", 17);
            pdfCtrl1.addCustomToolButton("适合宽度", "SetPageWidth()", 18);
            pdfCtrl1.addCustomToolButton("-", "", 0);
            pdfCtrl1.addCustomToolButton("首页", "FirstPage()", 8);
            pdfCtrl1.addCustomToolButton("上一页", "PreviousPage()", 9);
            pdfCtrl1.addCustomToolButton("下一页", "NextPage()", 10);
            pdfCtrl1.addCustomToolButton("尾页", "LastPage()", 11);
            pdfCtrl1.addCustomToolButton("-", "", 0);
            pdfCtrl1.addCustomToolButton("向左旋转90度", "SetRotateLeft()", 12);
            pdfCtrl1.addCustomToolButton("向右旋转90度", "SetRotateRight()", 13);*/
    		return pdfCtrl1;
    	}
    	
    }
    
    
  • 相关阅读:
    Collection
    DP
    JVM
    算法 *-* 并查集Union-Find(连通性)
    log4j
    log4j
    第254期:宠物如何导航回家
    第254期:宠物如何导航回家
    多线程
    多线程
  • 原文地址:https://www.cnblogs.com/sunBinary/p/11244908.html
Copyright © 2011-2022 走看看