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

📄 mixedfile.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package eti.bi.alphaminer.core.util;
/**
 * Created on 2006/08/03
 * @author Xiaojun Chen
 * MixedFile will take the jar file and zip file as a file or file set
 * */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Enumeration;
import java.util.Vector;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class MixedFile extends File{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see java.io.File#File(File, String)
	 * */
	public MixedFile(File parent, String child) {
		super(parent, child);
	}
	
	/**
	 * @see java.io.File#File(String)
	 * */
	public MixedFile(String pathname) {
		 super(pathname);
	}
	
	/**
	 * @see java.io.File#File(URI)
	 * */
	public MixedFile(URI uri) {
		super(uri);
	}
	
	/**
	 * @see java.io.File#File(String, String)
	 * */
	public MixedFile(String parent, String child) {
		super(parent,child);
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return vector of all file within this File in the style of MixedLocation that were accepted
     *          by the given filter
	 * */
	public Vector<MixedLocation> listAll(MixedFiletypeFileter mixedfilter){
		if(isDirectory()){
			return listAllFordir(mixedfilter);
		}
		else{
			return listAllForfile(mixedfilter);
		}
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return vector of all file within this File Directory in the style of MixedLocation that were accepted
     *          by the given filter
	 * */
	private Vector<MixedLocation> listAllFordir(MixedFiletypeFileter mixedfilter){
		Vector<MixedLocation> classes = new Vector<MixedLocation>();
		
		//direct file
		String[] ca = list(mixedfilter);
		
		int i,lastdot;
		String filename,suffix;
		Enumeration em;
		
		if(ca!=null){
			
			for(i=0;i<ca.length;i++){
				
				//file in zip file or jar file
				
				if(ca[i].endsWith(".zip")||ca[i].endsWith(".jar")){

					try {
						em = listEntryofZip(getAbsolutePath()+File.separator+ca[i]);
						if(em!=null){
							while(em.hasMoreElements()){
								filename = ((ZipEntry)em.nextElement()).getName();
								
								
								if(mixedfilter!=null){
									if(filename!=null){
										lastdot = filename.lastIndexOf(".");
										suffix = "";
										
										if(lastdot<filename.length()-1){
											suffix = filename.substring(lastdot+1);
											if(mixedfilter.accept(suffix)){
												classes.add(new MixedLocation(filename,ca[i]));
											}
										}
									}
								}
								else{
									classes.add(new MixedLocation(filename,ca[i]));
								}
							}
						}
					} catch (IOException e) {
						e.printStackTrace();
					}	
					
					continue;
				}
				
				classes.add(new MixedLocation(ca[i],""));
			}
			
		}
		
		
		
		return classes;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return vector of all file within this File in the style of MixedLocation that were accepted
     *          by the given filter
	 * */
	private Vector<MixedLocation> listAllForfile(MixedFiletypeFileter mixedfilter){
		Vector<MixedLocation> classes = new Vector<MixedLocation>();
		
		//direct file
		
		int lastdot;
		String filename,suffix,name;
		Enumeration em;
		
		filename = getName();
		name = getAbsolutePath();
		//file in zip file or jar file
		
		if(filename.endsWith(".zip")||filename.endsWith(".jar")){

			try {
				em = listEntryofZip(name);
				if(em!=null){
					while(em.hasMoreElements()){
						filename = ((ZipEntry)em.nextElement()).getName();
						
						if(mixedfilter!=null){
							if(filename!=null){
								lastdot = filename.lastIndexOf(".");
								suffix = "";
								
								if(lastdot<filename.length()-1){
									suffix = filename.substring(lastdot+1);
									if(mixedfilter.accept(suffix)){
										classes.add(new MixedLocation(filename,name));
									}
								}
							}
						}
						else{
							classes.add(new MixedLocation(filename,name));
						}
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}	
			
			return classes;
		}
		
		classes.add(new MixedLocation(name,""));
		
		return classes;
	}
	
	/**
	 * @param absolutefilename absolute file name
	 * @param mixedfilter file type filter
	 * @return vector of all file within jar file or zip file in the style of MixedLocation that were accepted
     *          by the given filter
	 * */
	public static Vector<MixedLocation> listZipandJar(String absolutefilename,MixedFiletypeFileter mixedfilter){
		if(absolutefilename==null){
			return null;
		}
		
		String suffix = getSuffix(absolutefilename);
		if(suffix==null||!(suffix.equals("zip")||suffix.equals("jar"))){
			return null;
		}
		
		Vector<MixedLocation> classes = new Vector<MixedLocation>();
		int lastdot;
		Enumeration em;
		String filename;
		
		try {
			em = listEntryofZip(absolutefilename);
			if(em!=null){
				while(em.hasMoreElements()){
					filename = ((ZipEntry)em.nextElement()).getName();
					if(mixedfilter!=null){
						if(filename!=null){
							lastdot = filename.lastIndexOf(".");
							suffix = "";
							
							if(lastdot<filename.length()-1){
								suffix = filename.substring(lastdot+1);
								if(mixedfilter.accept(suffix)){
									classes.add(new MixedLocation(filename,absolutefilename));
								}
							}
						}
					}
					else{
						classes.add(new MixedLocation(filename,absolutefilename));
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return classes;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return Arrsy of name of file within zip file of this file that were accepted
     *          by the given filter
	 * */
	public String[] listNameofAllZip(MixedFiletypeFileter mixedfilter){
		Vector<MixedEntryLocation> mel = listEntryofAllZip(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 zip file of this file that were accepted
     *          by the given filter
	 * */
	public Vector<MixedEntryLocation> listEntryofAllZip(MixedFiletypeFileter mixedfilter){
		if(isDirectory()){
			return listEntryofAllZipinDir(mixedfilter);
		}
		else{
			String filename = getName();
			if(filename!=null){
				if(filename.endsWith(".zip")){
					return listEntryofAllZipinfile(mixedfilter);
				}
			}
		}
		
		return null;
	}
	
	/**
	 * @param mixedfilter file type filter
	 * @return Vector of MixedEntryLocation of file within zip file of this file that were accepted
     *          by the given filter
	 * */
	private Vector<MixedEntryLocation> listEntryofAllZipinfile(MixedFiletypeFileter mixedfilter){
		Vector<MixedEntryLocation> files = new Vector<MixedEntryLocation>();
		Enumeration em;
		ZipFile zipFile;
		ZipEntry zipEntry;
		String filename,suffix;
		int lastdot;
		
		MixedEntryLocation mel = null;
		try {
			filename = getAbsolutePath();
			zipFile = new ZipFile(getAbsoluteFile());
			em = zipFile.entries();
			if(em!=null){
				mel = new MixedEntryLocation(filename);
				while(em.hasMoreElements()){
					zipEntry = ((ZipEntry)em.nextElement());
					filename = zipEntry.getName();

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

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

		
		return files;
	}	
	
	/**
	 * @param mixedfilter file type filter
	 * @return Enumeration of ZipEntry within zip file of this file that were accepted
     *          by the given filter
	 * */
	@SuppressWarnings("unchecked")
	public static Enumeration<ZipEntry> listEntryofZip(String absolutezipname) throws IOException{
		if(absolutezipname==null){
			return null;
		}
		
		ZipFile zipFile = new ZipFile(absolutezipname);
		
		return (Enumeration<ZipEntry>) zipFile.entries();
	}
	
	/**
	 * @param ml MixedLocation
	 * @return ZipEntry in the given location within the MixedLocation ml
	 * @throws IOException 
	 * */
	public ZipEntry getZipEntry(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;
		}

⌨️ 快捷键说明

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