Home Backend Development PHP Tutorial Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php

Jun 23, 2016 pm 01:48 PM
c# php zip optimal unzip

Atitit. 解压缩zip文件 的实现最佳实践 java c# .net php  

 

 

 

1. Jdk zip 跟apache ant zip 1

2. Apache Ant包进行ZIP文件压缩,upzip 大概流程.. 1

3. 读文件名称ok,但是cant读取到input说NPE.. 2

4. Ant1.8.2.jar 2

5. #---详细code 2

6. 参考 4

 

1.   Jdk zip 跟apache ant zip

 

下面实现的功能是zip文件中的图像文件解压到当前目录下,用jdk自带的处理zip文件的代码处理的,但是不能处理中文名称的文件,要不然就会出错。

 

下面是用的apache的zip文件处理包进行处理的,可以处理中文名称的文件,功能跟上面的一样。

使用apache ant version1.7的tools.zip来解压zip文件,解决中文问题

1.7 blow的好像还是不支持中文..

作者:: 老哇的爪子 Attilax 艾龙,  EMAIL:1466519819@qq.com

 

2. Apache Ant包进行ZIP文件压缩,upzip 大概流程..

许多年前就遇到过这种业务,对ZIP标准压缩文件解压。之前写的操作类现在找不到了,最近项目中又要处理这种业务,所以重新写了一个。Java提供 了处理ZIP包的API。但是对中文支持不是很好,所以我直接用Apache Ant里的ZIP操作API来进行处理。ANT的API解决了中文支持问题,而且用起来也非常方便。以下是操作类。

以下的类只是用到Apache的一小部分功能。具体更多的API,请参考文档。在此不多说明了。

* 在项目中导入Apache的ant.jar包到Lib中

 

 

 

3. 读文件名称ok,但是cant读取到input说NPE..

 

Cause:::encode问题.. 默认好像是utf8..but 实际是gbk... 

org.apache.tools.zip.ZipFile zipFile =new ZipFile(zipFileName, "gbk");

 

二、在unZipFiles方法中直接使用ZipFile zip = new ZipFile(zipFile); 解压缩时发现中文仍然乱码,改成ZipFile zip = new ZipFile(zipFile,“GBK”); 后中文正常了,可能和项目具体配置与运行环境有关吧。

 

4. Ant1.8.2.jar

 

5. #---详细code

/**

 * 解压静态方法

 * @param zipFileName

 * @param outputDirectory

 * @throws Exception

 */

public static void extract(String zipFileName,String outputDirectory,String encode) throws Exception{

try {

// = "utf-8";

org.apache.tools.zip.ZipFile zipFile =new ZipFile(zipFileName, encode);

//new org.apache.tools.zip.ZipFile(zipFileName);

java.util.Enumeration e = zipFile.getEntries();

 

org.apache.tools.zip.ZipEntry zipEntry = null;

 

while (e.hasMoreElements()){

zipEntry = (ZipEntry)e.nextElement();

 System.out.println("unziping "+zipEntry.getName());

 try {

 upzip(outputDirectory, zipFile, zipEntry);

} catch (zipEntryIsNullEx e2) {

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

 System.out.println("------------");

}

}

}

catch (Exception ex){

System.out.println("解压文件异常"+ex.getMessage());

ex.printStackTrace();

}

}

private static void upzip(String outputDirectory, org.apache.tools.zip.ZipFile zipFile, org.apache.tools.zip.ZipEntry zipEntry) throws  IOException, ZipException, FileNotFoundException, zipEntryIsNullEx {

if (zipEntry.isDirectory()){

String name=zipEntry.getName();

name=name.substring(0,name.length()-1);// for del fesyegeor

mkDirs(outputDirectory+File.separator+name);

//System.out.println("创建目录:"+outputDirectory+File.separator+name);

 

}else{  //file entry o9o

String name=zipEntry.getName();

String dir = name.substring(0,name.lastIndexOf("/"));

mkDirs(outputDirectory+File.separator+dir);

//System.out.println("创建文件:"+outputDirectory+File.separator+name);

File f=new File(outputDirectory+File.separator+zipEntry.getName());

f.createNewFile();

InputStream in = zipFile.getInputStream(zipEntry);

if(in==null)

throw new zipEntryIsNullEx("zipEntryIsNullEx:"+name);

FileOutputStream out=new FileOutputStream(f);

int c;

byte[] by=new byte[1024];

while((c=in.read(by)) != -1){

out.write(by,0,c);

}

out.close();

in.close();

}

}

 

6. 参考

 

Apache Ant包进行ZIP文件压缩 - 抹去浮华,沉淀深度 - ITeye技术网站.htm

基于apache zip包的压缩和解压缩程序_Crusoe_新浪博客

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1264
29
C# Tutorial
1237
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

See all articles