getsize java_Java ZipEntry getSize()用法及代码示例

getSize()函数是java.util.zip软件包的一部分。该函数返回作为参数传递的特定ZipEntry的未压缩大小,如果未知则返回-1。函数签名:

public long getSize()

用法:

zip_entry.getSize();

参数:此功能不需要任何参数。

返回值:该函数返回一个long值,即此ZipEntry的未压缩大小。

Exceptions:该函数不会引发任何异常。

以下示例程序旨在说明getSize()函数的使用

范例1:我们将创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目,然后获取指定ZipEntry的未压缩大小。“ file.zip”是f:目录中的一个zip文件。我们将以“.zip”文件作为ZipEntry

// Java program to demonstrate the

// use of getSize() function

import java.util.zip.*;

import java.util.Enumeration;

import java.util.*;

import java.io.*;

public class solution {

public static void main(String args[])

{

try {

// Create a Zip File

ZipFile zip_file = new ZipFile("f:\\file1.zip");

// get the Zip Entry using

// the getEntry() function

ZipEntry entry = zip_file.getEntry("file.zip");

// Get the uncompressed Size

// using the getSize()

// function

long input = entry.getSize();

// Display the uncompressed Size

System.out.println("uncompressed Size:" + input);

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

输出:

uncompressed Size:2153

范例2:我们将创建一个名为zip_file的文件,并使用getEntry()函数获取zip文件条目,然后获取指定ZipEntry的未压缩大小。“ file.zip”是f:目录中的一个zip文件。我们将以“.cpp”文件作为ZipEntry

// Java program to demonstrate the

// use of getSize() function

import java.util.zip.*;

import java.util.Enumeration;

import java.util.*;

import java.io.*;

public class solution {

public static void main(String args[])

{

try {

// Create a Zip File

ZipFile zip_file = new ZipFile("f:\\file1.zip");

// get the Zip Entry using

// the getEntry() function

ZipEntry entry = zip_file.getEntry("file1.cpp");

// Get the uncompressed Size

// using the getSize()

// function

long input = entry.getSize();

// Display the uncompressed Size

System.out.println("uncompressed Size:" + input);

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

输出:

uncompressed Size:783


版权声明:本文为weixin_36132740原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。