日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java定义一个getsize方法,long getSize()

發布時間:2024/4/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java定义一个getsize方法,long getSize() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

long getSize()

描述 (Description)

java.util.zip.ZipEntry.getSize()方法返回條目數據的未壓縮大小,如果未知,則返回-1。

聲明 (Declaration)

以下是java.util.zip.ZipEntry.getSize()方法的聲明。public long getSize()

返回值 (Returns)

條目數據的未壓縮大小,如果未知則為-1。

先決條件

在D:》 test 》目錄中創建一個文件Hello.txt,其中包含以下內容。This is an example.

例子 (Example)

以下示例顯示了java.util.zip.ZipEntry.getSize()方法的用法。package com.iowiki;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.Enumeration;

import java.util.zip.Adler32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

public class ZipEntryDemo {

private static String SOURCE_FILE = "D:\\test\\Hello.txt";

private static String TARGET_FILE = "D:\\test\\Hello.zip";

public static void main(String[] args) {

try {

createZipFile();

readZipFile();

} catch(IOException ioe) {

System.out.println("IOException : " + ioe);

}

}

private static void createZipFile() throws IOException{

FileOutputStream fout = new FileOutputStream(TARGET_FILE);

CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32());

ZipOutputStream zout = new ZipOutputStream(checksum);

FileInputStream fin = new FileInputStream(SOURCE_FILE);

ZipEntry zipEntry = new ZipEntry(SOURCE_FILE);

zout.putNextEntry(zipEntry);

int length;

byte[] buffer = new byte[1024];

while((length = fin.read(buffer)) > 0) {

zout.write(buffer, 0, length);

}

zout.closeEntry();

fin.close();

zout.close();

}

private static void readZipFile() throws IOException{

final ZipFile file = new ZipFile(TARGET_FILE);

System.out.println("Iterating over zip file : " + TARGET_FILE);

try {

final Enumeration extends ZipEntry> entries = file.entries();

while (entries.hasMoreElements()) {

final ZipEntry entry = entries.nextElement();

System.out.printf("File: %s Size %d Modified on %TD %n",

entry.getName(), entry.getSize(),

new Date(entry.getTime()));

extractFile(entry, file.getInputStream(entry));

}

System.out.printf("Zip file %s extracted successfully.", SOURCE_FILE);

}

finally {

file.close();

}

}

private static void extractFile(final ZipEntry entry, InputStream is)

throws IOException {

FileOutputStream fos = null;

try {

fos = new FileOutputStream(entry.getName());

final byte[] buf = new byte[1024];

int read = 0;

int length;

while ((length = is.read(buf, 0, buf.length)) >= 0) {

fos.write(buf, 0, length);

}

} catch (IOException ioex) {

fos.close();

}

}

}

讓我們編譯并運行上面的程序,這將產生以下結果 -Iterating over zip file : D:\test\Hello.zip

File: D:\test\Hello.txt Size 19 Modified on 05/20/17

Zip file D:\test\Hello.txt extracted successfully.

總結

以上是生活随笔為你收集整理的java定义一个getsize方法,long getSize()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。