Contoh ini menggunakan stream input dan output ZIP Java untuk mengompres dan mendekompres file. Bagian sebelumnya dari kode mengimplementasikan akuisisi jalur file, perubahan pada nama file terkompresi, dll., Sebagai berikut:
Paket com.utility.zip; impor java.io.bufferedInputStream; impor java.iufferedoutputstream; impor java.io.file; import java.io.fileInputStream; impor java.io.fileOutputStream; Impor java.io.fileoutputstream; impor com.fileomplam; Impor Com.FileOMS; Impor COM.FILEOT; Java's Zip input and output stream* * @author liujiduo * */public final class ZipUtil {private ZipUtil() {// empty}/** * Compressed file* * @param filePath * File path to be compressed* @return Compressed file*/public static File zip(String filePath) {File target = null;File source = new File(filePath);if (Source.exists ()) {// Nama file terkompresi = sumber fileName.zipstring zipName = source.getName () + ".zip"; target = file baru (source.getParent (), zipname); if target.exists ()) {target.delete (); // hapus file}} noloUpream = noUs = noloUpstream; target = noUput; target. {fos = new FileOutputStream(target);zos = new ZipOutputStream(new BufferedOutputStream(fos));// Add the corresponding file EntryaddEntry("/", source, zos);}catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zos, fos);}}return Target;}/** * Pindai untuk menambahkan entri file * * @param basis * Jalur dasar * @param sumber * File sumber * @param zos * aliran output file zip * @throws ioException */private static addentry (string basis, sumber, sourkors = s luchb. Source.getName (); if (source.isdirectory ()) {for (file file: source.listFiles ()) {// Sebutkan semua file dalam direktori dan tambahkan file entryaddentry (entri + "/, file, newerpeMer42 {new -new; new (new (new -neWREnpute, {new (new -neWREInpuREM = {NEWLInputstream; * 10]; fis = FileInputStream baru (sumber); bis = baru bufferedInputStream (fis, buffer.length); int read = 0; zos.putNextEntry (zipentry baru (entri)); while ((read = bis.read(buffer, 0, buffer.length)) != -1) {zos.write(buffer, 0, read);}zos.closeEntry();} finally {IOUtil.closeQuietly(bis, fis);}}}/** * Unzip file* * @param filePath * Compress file path*/public static void unzip(String filepath) {file sumber = file baru (filepath); jika (sumber entry.getName());if (!target.getParentFile().exists()) {// Create the parent directory of the file target.getParentFile().mkdirs();}// Write to the file bos = new BufferedOutputStream(new FileOutputStream(target));int read = 0;byte[] buffer = new byte[1024 * 10];while ((read = zis.read(buffer, 0, buffer.length)) != -1) {bos.write(buffer, 0, read);}bos.flush();}zis.closeEntry();}catch (IOException e) {throw new RuntimeException(e);} finally {IOUtil.closeQuietly(zis, bos);}}} public static void main (string [] args) {string targetPath = "e: // win7 wallpaper"; file file = ziputil.zip (targetPath); System.out.println (file); ziputil.unzip ("f: // win7 wallpaper.zip");}}Berikut ini adalah deskripsi bahasa Java untuk menutup satu atau lebih objek aliran melalui kelas alat IO Stream untuk mendapatkan daftar objek aliran tertutup, sebagai berikut:
Paket com.utility.io; import java.io.closable; import java.io.ioException;/** * io stream tool class * * @author liujiduo * */kelas publik ioutil {/** * tutup satu atau lebih objek aliran * @param closeables * daftar objek aliran tertutup * @throws ioExcepte ioExcepte * @param closeables * Daftar objek aliran tertutup * @throws ioeAccepte ioExcept * @param closeables * Daftar objek aliran tertutup * @throws ioeAbcle ioExcepte ioExcepte * @param closebles * Daftar objek aliran tertutup * @throws ioExcepte ioExcepte IoExcept * IOException {if (closeables != null) {for (Closeable closeable : closeables) {if (closeable != null) {closeable.close();}}}}}/** * Close one or more stream objects* * @param closeables * List of stream objects that can be closed*/public static void closeQuietly(Closeable... closeables) {try {tutup (closeAbles);} catch (ioException e) {// Do Nothing}}}Meringkaskan
Di atas adalah seluruh konten dari artikel ini tentang contoh kode dari metode penggunaan alat ziputil Java, saya harap ini akan membantu semua orang. Teman yang tertarik dapat terus merujuk ke topik terkait lainnya di situs ini. Jika ada kekurangan, silakan tinggalkan pesan untuk menunjukkannya.