⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myutils.java

📁 EXTJS做的网络硬盘系统..........
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				if (new_h > height) {
					tempdouble = new_h / height;
					new_w = Math.round(new_w / tempdouble);
					new_h = Math.round(new_h / tempdouble);
				}
			}
			BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // 绘制缩小后的图
			FileOutputStream newimage = new FileOutputStream(file); // 输出到文件流
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
			param.setQuality((float) (100 / 100.0), true);// 设置图片质量,100最大,默认70
			encoder.encode(tag, param);
			encoder.encode(tag); // 将JPEG编码
			newimage.close();
		}
	}

	/**
	 * 判断文件类型是否是合法的,就是判断allowTypes中是否包含contentType
	 * 
	 * @param contentType
	 *            文件类型
	 * @param allowTypes
	 *            文件类型列表
	 * @return 是否合法
	 */
	public static boolean isValid(String contentType, String[] allowTypes) {
		if (null == contentType || "".equals(contentType)) {
			return false;
		}
		for (String type : allowTypes) {
			if (contentType.equals(type)) {
				return true;
			}
		}
		return false;
	}


	/**
	 * 多文件压缩
	 * 
	 * <pre>
	 *    Example : 
	 *    ZipOutputStream zosm = new ZipOutputStream(new FileOutputStream(&quot;c:/b.zip&quot;));
	 *    zipFiles(zosm, new File(&quot;c:/com&quot;), &quot;&quot;);
	 *    zosm.close();
	 * </pre>
	 * 
	 * @param zosm
	 * @param file
	 * @param basePath
	 * @throws IOException
	 */
	public static void compressionFiles(ZipOutputStream zosm, File file, String basePath) {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			try {
				zosm.putNextEntry(new ZipEntry(basePath + "/"));
			} catch (IOException e) {
				e.printStackTrace();
			}
			basePath = basePath + (basePath.length() == 0 ? "" : "/") + file.getName();
			for (File f : files) {
				compressionFiles(zosm, f, basePath);
			}
		} else {
			FileInputStream fism = null;
			BufferedInputStream bism = null;
			try {
				byte[] bytes = new byte[1024];
				fism = new FileInputStream(file);
				bism = new BufferedInputStream(fism, 1024);
				basePath = basePath + (basePath.length() == 0 ? "" : "/") + file.getName();
				zosm.putNextEntry(new ZipEntry(basePath));
				int count;
				while ((count = bism.read(bytes, 0, 1024)) != -1) {
					zosm.write(bytes, 0, count);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (bism != null) {
					try {
						bism.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fism != null) {
					try {
						fism.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	/**
	 * 解压缩zip文件
	 * 
	 * @param zipFileName
	 *            压缩文件
	 * @param extPlace
	 *            解压的路径
	 */
	public static boolean decompressionZipFiles(String zipFileName, String extPlace) {
		boolean flag = false;
		try {
			unZip(zipFileName,extPlace);
			flag = true;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return flag;
//		java.util.zip.ZipInputStream in = null; 
//		java.util.zip.ZipEntry entry = null;
//		FileOutputStream os = null;
//		try {
//			in = new java.util.zip.ZipInputStream(new FileInputStream(zipFileName));
//			while ((entry = in.getNextEntry()) != null) {
//				String entryName = entry.getName();
//				int end = entryName.lastIndexOf("/");
//				String name = "";
//				if (end != -1) {
//					name = entryName.substring(0, end);
//				}
//				File file = new File(extPlace + name);
//				if (!file.exists()) {
//					file.mkdirs();
//				}
//				if (entry.isDirectory()) {
//					in.closeEntry();
//					continue;
//				} else {
//					os = new FileOutputStream(extPlace + entryName);
//					byte[] buf = new byte[1024];
//					int len;
//					while ((len = in.read(buf)) > 0) {
//						os.write(buf, 0, len);
//					}
//					in.closeEntry();
//				}
//			}
//			flag = true;
//		} catch (FileNotFoundException e1) {
//			flag = false;
//			e1.printStackTrace();
//		} catch (IOException e1) {
//			flag = false;
//			e1.printStackTrace();
//		} finally {
//			if (in != null) {
//				try {
//					in.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//			if (os != null) {
//				try {
//					os.close();
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
//			}
//		}
	}

	/**
	 * 解压缩rar文件
	 * 
	 * @param rarFileName
	 * @param extPlace
	 */
	public static boolean decompressionRarFiles(String rarFileName, String extPlace) {
		boolean flag = false;
		Archive archive = null;
		File out = null;
		File file = null;
		File dir = null;
		FileOutputStream os = null;
		FileHeader fh = null;
		String path, dirPath = "";
		try {
			file = new File(rarFileName);
			archive = new Archive(file);
		} catch (RarException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		} finally {
			if (file != null) {
				file = null;
			}
		}
		if (archive != null) {
			try {
				fh = archive.nextFileHeader();
				while (fh != null) {
					path = (extPlace + fh.getFileNameString().trim()).replaceAll("\\\\", "/");
					int end = path.lastIndexOf("/");
					if (end != -1) {
						dirPath = path.substring(0, end);
					}
					try {
						dir = new File(dirPath);
						if (!dir.exists()) {
							dir.mkdirs();
						}
					} catch (RuntimeException e1) {
						e1.printStackTrace();
					} finally {
						if (dir != null) {
							dir = null;
						}
					}
					if (fh.isDirectory()) {
						fh = archive.nextFileHeader();
						continue;
					}
					out = new File(extPlace + fh.getFileNameString().trim());
					try {
						os = new FileOutputStream(out);
						archive.extractFile(fh, os);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (RarException e) {
						e.printStackTrace();
					} finally {
						if (os != null) {
							try {
								os.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
						if (out != null) {
							out = null;
						}
					}
					fh = archive.nextFileHeader();
				}
			} catch (RuntimeException e) {
				e.printStackTrace();
			} finally {
				fh = null;
				if (archive != null) {
					try {
						archive.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			flag = true;
		}
		return flag;
	}

	private static void getDir(String directory, String subDirectory){
	     String dir[];
	     File fileDir = new File(directory);
	     try {
	      if (subDirectory == "" && fileDir.exists() != true)
	       fileDir.mkdir();
	      else if (subDirectory != "") {
	       dir = subDirectory.replace('\\', '/').split("/");
	       for (int i = 0; i < dir.length; i++) {
	        File subFile = new File(directory + File.separator + dir[i]);
	        if (subFile.exists() == false)
	         subFile.mkdir();
	        directory += File.separator + dir[i];
	       }
	      }
	     }catch (Exception ex) {
	       System.out.println(ex.getMessage());
	     }
	 }
	 /**
	  *
	  * @param zipFileNaame      being unzip file including  file name and path ;   
	  * @param outputDirectory    unzip files to this directory
	  *
	  */ 
	 public static  void unZip(String zipFileName, String outputDirectory){
	     try {
	      ZipFile zipFile = new ZipFile(zipFileName);
	      java.util.Enumeration e = zipFile.getEntries();
	      ZipEntry zipEntry = null;
	      getDir(outputDirectory, "");
	      while (e.hasMoreElements()) {
	       zipEntry = (ZipEntry) e.nextElement();
	       //System.out.println("unziping " + zipEntry.getName());
	         if (zipEntry.isDirectory()) {                //如果得到的是个目录,
	          String name = zipEntry.getName();         //  就创建在指定的文件夹下创建目录
	           name = name.substring(0, name.length() - 1);
	          File f = new File(outputDirectory + File.separator + name);
	          f.mkdir();
	          //System.out.println("创建目录:" + outputDirectory + File.separator + name);
	         }
	         else {
	          String fileName = zipEntry.getName();
	          fileName = fileName.replace('\\', '/');
	          // System.out.println("测试文件1:" +fileName);
	          if (fileName.indexOf("/") != -1){
	           getDir(outputDirectory,
	                               fileName.substring(0, fileName.lastIndexOf("/")));
	           //System.out.println("文件的路径:"+fileName);
	           fileName=fileName.substring(fileName.lastIndexOf("/")+1,fileName.length());
	              
	          }

	             File f = new File(outputDirectory + File.separator + zipEntry.getName());

	             f.createNewFile();
	             InputStream in = zipFile.getInputStream(zipEntry);
	             FileOutputStream out=new FileOutputStream(f);

	             byte[] by = new byte[1024];
	             int c;
	             while ( (c = in.read(by)) != -1) {
	              out.write(by, 0, c);
	           }
	           out.close();
	           in.close();
	         }
	       }
	     }catch (Exception ex) {
	       System.out.println(ex.getMessage());
	     }
	        
	 }
	 /**
	  * this mothed will unzip all the files  which in your specifeid  folder;
	  * @param filesFolder   
	  * @param outputDirectory       
	  */
	 public static  void unzipFiles(String filesFolder ,String outputDirectory){
	  File zipFolder=new File (filesFolder);
	  String zipFiles [];
	  String zipFileAbs;
	  try{
	   zipFiles=zipFolder.list();
	   for(int i=0;i<zipFiles.length;i++){
	    if(zipFiles[i].length()==(zipFiles[i].lastIndexOf(".zip")+4)){//判断是不是zip包 
	     zipFileAbs=filesFolder+File.separator+zipFiles[i];
	     unZip(zipFileAbs,outputDirectory);
	    }
	   }
	  }catch (SecurityException ex){
	   ex.printStackTrace();
	  }
	    
	 }
	 
	public static void main(String[] ar) throws RarException, IOException {
		String s = null;
		s.length();
		unzipFiles("K:\\song","K:\\song\\song");
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -