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

📄 jarextractor.java

📁 Ftp服务1.0
💻 JAVA
字号:
package ranab.jar;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.jar.JarFile;


/**
 * This is JAR file extractor class. It extracts the JAR file
 * in a separate thread. Where we are passing a MyJarObserver
 * object to track the current status of this decompression.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
abstract class JarExtractor implements Runnable {

    protected File mDir;
    protected File mJarFile;
    protected JarObserverContainer mObserverCont;
    protected boolean mbIsPauseRequest = false;
    protected boolean mbIsStopRequest = false;


    public JarExtractor(File jarFile, File dir) {
        mDir = dir;
        mJarFile = jarFile;
        mObserverCont = new JarObserverContainer();
    }

    /**
     * add observer
     */
    public void addObserver(JarObserver obsr)  {
        mObserverCont.addObserver(obsr);
    } 
     
     
    /**
     * remove observer
     */
    public void removeObserver(JarObserver obsr)  {
        mObserverCont.removeObserver(obsr);
    }
    

    /**
     * Extract an entry from the zip file.
     */
    public void extract(ZipFile jf, ZipEntry ze) throws Exception {
        File fl = new File(mDir, ze.toString());

        // directory create it
        if (ze.isDirectory()) {
            fl.mkdirs();
            return;
        }

        File par = new File(fl.getParent());
        if (!par.exists()) {
            par.mkdirs();
        }

        // file decompres it
        FileOutputStream fos = new FileOutputStream(fl);
        extract(jf, ze, fos);
        fos.close();
    }


    /**
     * write ZipEntry into OutputStream
     */
    public static void extract (ZipFile jf, ZipEntry ze, OutputStream out) 
    throws Exception {

        InputStream is = jf.getInputStream(ze);
        byte buff[] = new byte[1024];
        int cnt = -1;
        while ((cnt = is.read(buff)) != -1) {
            out.write(buff, 0, cnt);
        }
        is.close();
    }



    /**
     * stop decompression
     */
    public void stop() {
        mbIsStopRequest = true;
    }

    public boolean isStopped() {
        return mbIsStopRequest;
    }

    /**
     * pause decompression
     */
    public void pause() {
        mbIsPauseRequest = true;
    }

    public boolean isPaused() {
        return mbIsPauseRequest;
    }

    /**
     * resume decompression
     */
    public void resume() {
        mbIsPauseRequest = false;
    }
    
    /**
     * get jar file name
     */
    public String toString() {
        return mJarFile.getAbsolutePath();
    }

    
    /**
     * start decompression
     */
    public abstract void extract();

}

⌨️ 快捷键说明

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