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

📄 mixedfile.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		ZipEntry zipentry = zipfile.getEntry(ml.name);
		return zipentry;

	}
	
	/**
	 * @param ml MixedLocation
	 * @return the input stream for reading the contents of the specified
     * zip file from the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public InputStream getZipInputStream(MixedLocation ml) throws IOException{
		
		if(ml == null){
			return null;
		}
		
		if(!ml.location.endsWith(".zip")){
			return null;
		}
		
		ZipFile zipfile = new ZipFile(ml.location);
		if(zipfile==null){
			return null;
		}
		ZipEntry zipentry = zipfile.getEntry(ml.name);
		if(zipentry!=null){
			return zipfile.getInputStream(zipentry);
		}
		return null;
		
	}
	
	/**
	 * @param ml MixedLocation
	 * @return the byte array of the contents of the specified
     * zip file from the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public byte[] getZipByte(MixedLocation ml) throws IOException{
		
		if(ml == null){
			return null;
		}
		
		if(!ml.location.endsWith(".zip")){
			return null;
		}
		
		ZipFile zipfile = new ZipFile(ml.location);
		if(zipfile==null){
			return null;
		}
		ZipEntry zipentry = zipfile.getEntry(ml.name);
		
		if(zipentry==null){
			return null;
		}
		
		InputStream in = zipfile.getInputStream(zipentry);
		
		byte[] bytes = new byte[(int) zipentry.getSize()];
		int totalread=0,size = (int) zipentry.getSize();
		
		while(totalread<size){
			totalread += in.read(bytes, totalread, size-totalread);
		}
		
		return bytes;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return Arrays of name of file within jar file of this file that were accepted
     *          by the given filter
	 * */
	public String[] listNameofAllJar(MixedFiletypeFileter mixedfilter){
		Vector<MixedEntryLocation> mel = listEntryofAllJar(mixedfilter);
		if(mel==null){
			return null;
		}
		
		
		int size=mel.size(),total=0;
		int i,j,temp;
		
		for(i=0;i<size;i++){
			total += mel.get(i).entries.size();
		}
		
		String[] name = new String[total];
		
		total = 0;
		Vector<ZipEntry> entries;
		for(i=0;i<size;i++){
			entries = mel.get(i).entries;
			temp = entries.size();
			for(j=0;j<temp;j++){
				name[total++] = entries.get(j).getName();
			}
		}
		
		return name;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return Vector of MixedEntryLocation of file within jar file of this directory that were accepted
     *          by the given filter
	 * */
	public Vector<MixedEntryLocation> listEntryofAllJar(MixedFiletypeFileter mixedfilter){
		
		if(isDirectory()){
			return listEntryofAllJarinDir(mixedfilter);
		}
		else{
			String filename = getName();
			if(filename!=null){
				if(filename.endsWith(".jar")){
					return listEntryofAllJarinfile(mixedfilter);
				}
			}
		}
		
		return null;
	}	
	
	/**
	 * @param mixedfilter file type filter
	 * @return Vector of MixedEntryLocation of file within jar file of this file that were accepted
     *          by the given filter
	 * */
	private Vector<MixedEntryLocation> listEntryofAllJarinfile(MixedFiletypeFileter mixedfilter){
		Vector<MixedEntryLocation> files = new Vector<MixedEntryLocation>();
		Enumeration em;
		JarFile jarFile;
		JarEntry jarEntry;
		String filename,suffix;
		int lastdot;
		
		MixedEntryLocation mel = null;
		try {
			filename = getAbsolutePath();
			jarFile = new JarFile(getAbsoluteFile());
			em = jarFile.entries();
			if(em!=null){
				mel = new MixedEntryLocation(filename);
				while(em.hasMoreElements()){
					jarEntry = ((JarEntry)em.nextElement());
					filename = jarEntry.getName();

					if(filename!=null){
						lastdot = filename.lastIndexOf(".");
						suffix = "";
						
						if(lastdot<filename.length()-1){
							suffix = filename.substring(lastdot+1);
							if(mixedfilter.accept(suffix)){
								mel.addEntry(jarEntry);
							}
						}
					}
				}
				files.add(mel);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		return files;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return Vector of MixedEntryLocation of file within jar file of this directory that were accepted
     *          by the given filter
	 * */
	private Vector<MixedEntryLocation> listEntryofAllJarinDir(MixedFiletypeFileter mixedfilter){
		
		String[] ca = list();
		
		if(ca==null){
			return null;
		}
		
		Vector<MixedEntryLocation> files = new Vector<MixedEntryLocation>();
		Enumeration em;
		JarFile jarFile;
		JarEntry jarEntry;
		String filename,suffix;
		int i,lastdot;
		
		MixedEntryLocation mel = null;
		for(i=0;i<ca.length;i++){
			if(ca[i].endsWith(".jar")){
				try {
					filename = getAbsolutePath()+File.separator+ca[i];
					jarFile = new JarFile(filename);
					em = jarFile.entries();
					
					if(em!=null){
						mel = new MixedEntryLocation(filename);
						while(em.hasMoreElements()){
							jarEntry = ((JarEntry)em.nextElement());
							filename = jarEntry.getName();

							if(filename!=null){
								lastdot = filename.lastIndexOf(".");
								suffix = "";
								
								if(lastdot<filename.length()-1){
									suffix = filename.substring(lastdot+1);
									if(mixedfilter.accept(suffix)){
										mel.addEntry(jarEntry);
									}
								}
							}
						}
						files.add(mel);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				
			}
		}

		
		return files;
	}	
	
	/**
	 * @param mixedfilter file type filter
	 * @return Enumeration of JarEntry within jar file of this file that were accepted
     *          by the given filter
	 * */
	@SuppressWarnings("unchecked")
	public static Enumeration<JarEntry> listEntryofJar(String absolutejarname) throws IOException{
		if(absolutejarname==null){
			return null;
		}
		
		JarFile jarFile = new JarFile(absolutejarname);
		
		return (Enumeration<JarEntry>) jarFile.entries();
	}
	
	/**
	 * @param ml MixedLocation
	 * @return JarEntry in the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public JarEntry getJarEntry(MixedLocation ml) throws IOException{
		if(ml == null){
			return null;
		}
		
		if(!ml.location.endsWith(".jar")){
			return null;
		}
		
		JarFile jarfile = new JarFile(ml.location);
		if(jarfile==null){
			return null;
		}
		JarEntry jarentry = jarfile.getJarEntry(ml.name);
		return jarentry;
	}
	
	/**
	 * @param ml MixedLocation
	 * @return the input stream for reading the contents of the specified
     * jar file from the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public InputStream getJarInputStream(MixedLocation ml) throws IOException{
		
		if(ml == null){
			return null;
		}
		
		if(!ml.location.endsWith(".jar")){
			return null;
		}
		
		JarFile jarfile = new JarFile(ml.location);
		if(jarfile==null){
			return null;
		}
		JarEntry jarentry = jarfile.getJarEntry(ml.name);
		if(jarentry!=null){
			return jarfile.getInputStream(jarentry);
		}
		return null;
		
	}
	
	/**
	 * @param ml MixedLocation
	 * @return the byte array of the contents of the specified
     * jar file from the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public byte[] getJarByte(MixedLocation ml) throws IOException{
		
		if(ml == null){
			return null;
		}
		
		if(!ml.location.endsWith(".jar")){
			return null;
		}
		
		JarFile jarfile;
		
		jarfile = new JarFile(ml.location);
		if(jarfile==null){
			return null;
		}
		JarEntry jarentry = jarfile.getJarEntry(ml.name);
		if(jarentry==null){
			return null;
		}
		
		InputStream in = jarfile.getInputStream(jarentry);
		byte[] bytes = new byte[(int) jarentry.getSize()];
		int totalread=0,size = (int) jarentry.getSize();
		
		while(totalread<size){
			totalread += in.read(bytes, totalread, size-totalread);
		}
		
		return bytes;
	}
	
	/**
	 * @param filename the absolute file path
	 * @return the suffix of this file, "" if the input is a directory
	 * */
	public static String getSuffix(String filename){
		if(filename==null){
			return null;
		}
		
		int lastdot = filename.lastIndexOf(".");
		
		String suffix = "";
		if(lastdot<filename.length()-1){
			suffix = filename.substring(lastdot+1);
		}
		
		return suffix;
	}
	
	public static void main(String[] args){
		
		MixedFile mf = new MixedFile("D:\\class\\");
		MixedFiletypeFileter mixedfilter = new MixedFiletypeFileter();
		//mixedfilter.setNonAcceptType("class");
		mixedfilter.setAcceptType("class");
		Vector<MixedLocation> classes = mf.listAll(mixedfilter);
		
		MixedLocation ml;
		int size = classes.size();
		for(int i=0;i<size;i++){
			ml = classes.get(i);
			System.out.println("\t"+ml.name+"\t"+ml.location);
		}
		
		System.out.println();
		
		String[] name = mf.listNameofAllJar(mixedfilter);
		size = name.length;
		for(int i=0;i<size;i++){
			System.out.println("\t"+name[i]);
		}
		
		System.out.println();
		
		name= mf.listNameofAllZip(mixedfilter);
		size = name.length;
		for(int i=0;i<size;i++){
			System.out.println("\t"+name[i]);
		}
		
		ml = new MixedLocation("eti/bi/alphaminer/core/config/ConfigLoader.class","D:\\class\\alphaminer.jar");
		JarEntry je = null;
		try {
			je = mf.getJarEntry(ml);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.exit(0);
		}
		if(je!=null){
			System.out.println(je.getSize());
		}
		
	}
}
class MixedLocation{
	String name;
	//jar name if within a jar file, or zip name if within a zip file, or "" if just directly access
	String location;
	
	
	public MixedLocation(String name, String location){
		this.name = name;
		this.location = location;
	}
}
class MixedEntryLocation{
	Vector<ZipEntry> entries;
	//jar name if within a jar file, or zip name if within a zip file, or "" if just directly access
	String location;
	
	
	public MixedEntryLocation(String location){
		this.location = location;
		entries = new Vector<ZipEntry>();
	}
	
	public void addEntry(ZipEntry entry){
		this.entries.add(entry);
	}
}


⌨️ 快捷键说明

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