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

📄 ziptoolkit.java

📁 该系统是一个基于p2p的即时聊天系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            buffer.append(st.nextToken());        }        deleteFile(new File(buffer.toString()));        File[] files = file.listFiles();        if (files != null && files.length > 0) {            rewriteFile(files);        } else {            newFile(fileName);        }        deleteFile(file);    }    /**     * 重命名     *     * @param entry 待重命名文件或者目录所对应的实体     * @param name 新的名称     */    public void rename(ZipEntry entry, String name) {        File file = new File(WORKING_DIR);        extractFile(file, entries.toArray(), null);        StringBuffer buffer = new StringBuffer();        buffer.append(file.getAbsolutePath());        String last = "";        StringTokenizer st = new StringTokenizer(entry.getName(), DELIMITER1);        while (st.hasMoreTokens()) {            last = st.nextToken();            buffer.append(DELIMITER2);            buffer.append(last);        }        String newName = buffer.substring(0, buffer.length() - last.length());        (new File(buffer.toString())).renameTo(new File(newName + name));        rewriteFile(file.listFiles());        deleteFile(file);    }    /**     * 提取文件     *     * @param file 待提取文件的存放目录     * @param objs 待提取文件的实体列表     * @param omit 待提取文件的相对路径     */    public void extractFile(File file, Object[] objs, String omit) {        byte data[] = new byte[BUFFER_SIZE];        try {            ZipFile zipFile = new ZipFile(fileName);            for (int i = 0; i < objs.length; i++) {                ZipEntry zipEntry = zipFile.getEntry(objs[i].toString());                String name = zipEntry.getName();                if (omit != null) {                    name = name.substring(omit.length(), name.length());                }                File file2 = new File(file, (new File(name)).getPath());                if (zipEntry.isDirectory() == true) {                    file2.mkdirs();                } else {                    file2.getParentFile().mkdirs();                    BufferedInputStream bis = new BufferedInputStream(                        zipFile.getInputStream(zipEntry), BUFFER_SIZE);                    BufferedOutputStream bos = new BufferedOutputStream(                        new FileOutputStream(file2), BUFFER_SIZE);                    int count;                    while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) {                        bos.write(data, 0, count);                    }                    bos.close();                    bis.close();                }            }            zipFile.close();        } catch (IOException e) {            e.printStackTrace();        }    }    /*------------------------------------------------------------------------*     *                                私有方法                                *     *------------------------------------------------------------------------*/    /**     * 新建文件     *     * @param fileName 待新建文件的文件名     */    private void newFile(String fileName) {        entries.clear();        this.fileName = fileName;        try {            new FileOutputStream(fileName);        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 复制文件     *     * @param src 源文件     * @param dest 目标文件     */    private void copyFile(File src, File dest) {        byte data[] = new byte[BUFFER_SIZE];        try {            InputStream fis = new FileInputStream(src);            InputStream bis = new BufferedInputStream(fis, BUFFER_SIZE);            OutputStream fos = new FileOutputStream(dest);            OutputStream bos = new BufferedOutputStream(fos, BUFFER_SIZE);            int count;            while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) {                bos.write(data, 0, count);            }            bos.close();            fos.close();            bis.close();            fis.close();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 删除文件     *     * @param file 待删除的文件,如果是目录则删除整个目录     */    private void deleteFile(File file) {        if (file.isFile()) {            file.delete();        } else if (file.isDirectory()) {            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {                deleteFile(files[i]);            }            file.delete();        }    }    /**     * 重写文件     *     * @param files 重新写入Zip文件中的文件列表     */    private void rewriteFile(File[] files) {        entries.clear();        (new File(fileName)).delete();        parentDir = files[0].getParent();        try {            OutputStream fos = new FileOutputStream(fileName, true);            OutputStream bos = new BufferedOutputStream(fos);            ZipOutputStream zos = new ZipOutputStream(bos);            for (int i = 0; i < files.length; i++) {                if (files[i].isFile() == true) {                    zipFile(files[i], new File(""), zos);                } else if (files[i].isDirectory() == true) {                    try {                        ZipEntry entry =                            new ZipEntry(files[i].getName() + DELIMITER1);                        zos.putNextEntry(entry);                        zos.closeEntry();                        entries.add(entry);                    } catch (ZipException e) {                        e.printStackTrace();                    } catch (IOException e) {                        e.printStackTrace();                    }                    recursiveZip(files[i], new File(files[i].getName()), zos);                }            }            zos.close();            bos.close();            fos.close();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 压缩文件     *     * @param file 待压缩的文件     * @param currentDir 当前目录     * @param zos 指定的ZipOutputStream,用于将文件压缩后的数据写入其中     */    private void zipFile(File file, File currentDir, ZipOutputStream zos) {        try {            String relevantPath = "";            InputStream fis = new FileInputStream(file);            InputStream bis = new BufferedInputStream(fis, BUFFER_SIZE);            if (currentDir.getPath().length() == 0) {                relevantPath = file.getName();            } else {                try {                    StringBuffer buffer = new StringBuffer();                    buffer.append("file:");                    buffer.append(currentDir.getPath());                    buffer.append(file.separator);                    buffer.append(file.getName());                    relevantPath = (new URL(buffer.toString())).getPath();                } catch (MalformedURLException e) {                    e.printStackTrace();                }            }            ZipEntry entry = new ZipEntry(relevantPath);            File orginal = new File(parentDir, relevantPath);            entry.setSize(orginal.length());            zos.putNextEntry(entry);            entries.add(entry);            int count;            byte data[] = new byte[BUFFER_SIZE];            while ((count = bis.read(data, 0 , BUFFER_SIZE)) != -1) {                zos.write(data, 0, count);            }            bis.close();            fis.close();        } catch (IOException e) {            e.printStackTrace();        }    }    /**     * 递归压缩文件     *     * @param file 待压缩的文件     * @param currentDir 当前目录     * @param zos 指定的ZipOutputStream,用于将文件压缩后写入其中     */    private void recursiveZip(File file, File currentDir, ZipOutputStream zos) {        String relevantPath = "";        for (int i = 0; i < file.list().length; i++) {            File fileToBeZipped = file.listFiles()[i];            if (fileToBeZipped.isFile() == true) {                zipFile(fileToBeZipped, currentDir, zos);            } else if (fileToBeZipped.isDirectory() == true) {                try {                    try {                        StringBuffer buffer = new StringBuffer();                        buffer.append("file:");                        buffer.append(currentDir.getPath());                        buffer.append(file.separator);                        buffer.append(fileToBeZipped.getName());                        buffer.append(DELIMITER1);                        relevantPath = (new URL(buffer.toString())).getPath();                    } catch (MalformedURLException e) {                        e.printStackTrace();                    }                    ZipEntry entry = new ZipEntry(relevantPath);                    zos.putNextEntry(entry);                    zos.closeEntry();                    entries.add(entry);                } catch (ZipException e) {                    e.printStackTrace();                } catch (IOException e) {                    e.printStackTrace();                }                StringBuffer buffer = new StringBuffer();                buffer.append(currentDir.getPath());                buffer.append(file.separator);                buffer.append(fileToBeZipped.getName());                recursiveZip(fileToBeZipped, new File(buffer.toString()), zos);            }        }    }}

⌨️ 快捷键说明

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