解压文件 Unzip
来源:互联网 发布:js与或非逻辑符号 编辑:程序博客网 时间:2024/04/30 09:26
package com.scnu.zip;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* UnZip -- print or unzip a JAR or PKZIP file using java.util.zip.
Command-line
* version: extracts files.
*
* @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07
* 17:40:35 ian Exp $
*/
public class UnZip {
/** Constants for mode listing or mode extracting. */
public static final int LIST = 0, EXTRACT = 1;
/** Whether we are extracting or just printing TOC */
protected int mode = LIST;
/** The ZipFile that is used to read an archive */
protected ZipFile zippy;
/** The buffer for reading/writing the ZipFile data */
protected byte[] b;
/**
* Simple main program, construct an UnZipper, process each .ZIP file
from
* argv[] through that object.
*/
//java Unzip zipfile.zip
public static void main(String[] argv) {
UnZip u = new UnZip();
for (int i = 0; i < argv.length; i++) {
if ("-x".equals(argv[i])) {
u.setMode(EXTRACT);
continue;
}
String candidate = argv[i];
// System.err.println("Trying path " + candidate);
if (candidate.endsWith(".zip") || candidate.endsWith(".jar"))
u.unZip(candidate);
else
System.err.println("Not a zip file? " + candidate);
}
System.err.println("All done!");
}
/** Construct an UnZip object. Just allocate the buffer */
UnZip() {
b = new byte[8092];
}
/** Set the Mode (list, extract). */
protected void setMode(int m) {
if (m == LIST || m == EXTRACT)
mode = m;
}
/** Cache of paths we've mkdir()ed. */
protected SortedSet dirsMade;
/** For a given Zip file, process each entry. */
public void unZip(String fileName) {
dirsMade = new TreeSet();
try {
zippy = new ZipFile(fileName);
Enumeration all = zippy.entries();
while (all.hasMoreElements()) {
getFile((ZipEntry) all.nextElement());
}
} catch (IOException err) {
System.err.println("IO Error: " + err);
return;
}
}
protected boolean warnedMkDir = false;
/**
* Process one file from the zip, given its name. Either print the
name, or
* create the file on disk.
*/
protected void getFile(ZipEntry e) throws IOException {
String zipName = e.getName();
switch (mode) {
case EXTRACT:
if (zipName.startsWith("/")) {
if (!warnedMkDir)
System.out.println("Ignoring absolute paths");
warnedMkDir = true;
zipName = zipName.substring(1);
}
// if a directory, just return. We mkdir for every file,
// since some widely-used Zip creators don't put out
// any directory entries, or put them in the wrong place.
if (zipName.endsWith("/")) {
return;
}
// Else must be a file; open the file for output
// Get the directory part.
int ix = zipName.lastIndexOf('/');
if (ix > 0) {
String dirName = zipName.substring(0, ix);
if (!dirsMade.contains(dirName)) {
File d = new File(dirName);
// If it already exists as a dir, don't do anything
if (!(d.exists() && d.isDirectory())) {
// Try to create the directory, warn if it fails
System.out.println("Creating Directory: " + dirName);
if (!d.mkdirs()) {
System.err.println("Warning: unable to mkdir "
+ dirName);
}
dirsMade.add(dirName);
}
}
}
System.err.println("Creating " + zipName);
FileOutputStream os = new FileOutputStream(zipName);
InputStream is = zippy.getInputStream(e);
int n = 0;
while ((n = is.read(b)) > 0)
os.write(b, 0, n);
is.close();
os.close();
break;
case LIST:
// Not extracting, just list
if (e.isDirectory()) {
System.out.println("Directory " + zipName);
} else {
System.out.println("File " + zipName);
}
break;
default:
throw new IllegalStateException("mode value (" + mode + ") bad");
}
}
}
运行: java UnZip -x zipfile.zip
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* UnZip -- print or unzip a JAR or PKZIP file using java.util.zip.
Command-line
* version: extracts files.
*
* @author Ian Darwin, Ian@DarwinSys.com $Id: UnZip.java,v 1.7 2004/03/07
* 17:40:35 ian Exp $
*/
public class UnZip {
/** Constants for mode listing or mode extracting. */
public static final int LIST = 0, EXTRACT = 1;
/** Whether we are extracting or just printing TOC */
protected int mode = LIST;
/** The ZipFile that is used to read an archive */
protected ZipFile zippy;
/** The buffer for reading/writing the ZipFile data */
protected byte[] b;
/**
* Simple main program, construct an UnZipper, process each .ZIP file
from
* argv[] through that object.
*/
//java Unzip zipfile.zip
public static void main(String[] argv) {
UnZip u = new UnZip();
for (int i = 0; i < argv.length; i++) {
if ("-x".equals(argv[i])) {
u.setMode(EXTRACT);
continue;
}
String candidate = argv[i];
// System.err.println("Trying path " + candidate);
if (candidate.endsWith(".zip") || candidate.endsWith(".jar"))
u.unZip(candidate);
else
System.err.println("Not a zip file? " + candidate);
}
System.err.println("All done!");
}
/** Construct an UnZip object. Just allocate the buffer */
UnZip() {
b = new byte[8092];
}
/** Set the Mode (list, extract). */
protected void setMode(int m) {
if (m == LIST || m == EXTRACT)
mode = m;
}
/** Cache of paths we've mkdir()ed. */
protected SortedSet dirsMade;
/** For a given Zip file, process each entry. */
public void unZip(String fileName) {
dirsMade = new TreeSet();
try {
zippy = new ZipFile(fileName);
Enumeration all = zippy.entries();
while (all.hasMoreElements()) {
getFile((ZipEntry) all.nextElement());
}
} catch (IOException err) {
System.err.println("IO Error: " + err);
return;
}
}
protected boolean warnedMkDir = false;
/**
* Process one file from the zip, given its name. Either print the
name, or
* create the file on disk.
*/
protected void getFile(ZipEntry e) throws IOException {
String zipName = e.getName();
switch (mode) {
case EXTRACT:
if (zipName.startsWith("/")) {
if (!warnedMkDir)
System.out.println("Ignoring absolute paths");
warnedMkDir = true;
zipName = zipName.substring(1);
}
// if a directory, just return. We mkdir for every file,
// since some widely-used Zip creators don't put out
// any directory entries, or put them in the wrong place.
if (zipName.endsWith("/")) {
return;
}
// Else must be a file; open the file for output
// Get the directory part.
int ix = zipName.lastIndexOf('/');
if (ix > 0) {
String dirName = zipName.substring(0, ix);
if (!dirsMade.contains(dirName)) {
File d = new File(dirName);
// If it already exists as a dir, don't do anything
if (!(d.exists() && d.isDirectory())) {
// Try to create the directory, warn if it fails
System.out.println("Creating Directory: " + dirName);
if (!d.mkdirs()) {
System.err.println("Warning: unable to mkdir "
+ dirName);
}
dirsMade.add(dirName);
}
}
}
System.err.println("Creating " + zipName);
FileOutputStream os = new FileOutputStream(zipName);
InputStream is = zippy.getInputStream(e);
int n = 0;
while ((n = is.read(b)) > 0)
os.write(b, 0, n);
is.close();
os.close();
break;
case LIST:
// Not extracting, just list
if (e.isDirectory()) {
System.out.println("Directory " + zipName);
} else {
System.out.println("File " + zipName);
}
break;
default:
throw new IllegalStateException("mode value (" + mode + ") bad");
}
}
}
运行: java UnZip -x zipfile.zip
- 解压文件 Unzip
- unzip解压中文名称文件
- unzip解压所以文件
- 解压.zip文件命令:unzip
- Linux unzip --解压zip文件
- unzip解压文件乱码问题解决
- unzip 解压大文件失败的解决办法
- unzip命令解压zip文件中文乱码
- Linux unzip解压多个文件
- gunzip 和 unzip 解压文件到指定的目录
- linux zip/unzip err encoding压缩解压中文文件 乱码
- gunzip 和 unzip 解压文件到指定的目录
- [转]gunzip 和 unzip 解压文件到指定的目录
- gunzip 和 unzip 解压文件到指定的目录
- 解决unzip不能解压windows创建的zip文件问题
- 解压zip文件出现-bash:unzip:commond not found
- Linux unzip解压文件到某个目录下面
- Java 文件压缩和解压 Zip and Unzip
- 如何提高新站的流量
- 刚到的新人点个到
- C#中接口的作用
- 华为面试题
- 内网服务应用—无需端口映射实现从外网访问控制内网电脑
- 解压文件 Unzip
- RS-232、RS-485和S-422的区别
- 读取ROM-BIOS
- 想用心学.NET才是正途
- Java容器类List、ArrayList、Vector及map、HashTable、HashMap的使用与区别
- エクセルvba 参考
- CSS对IE7,IE6,FireFox和其它不同浏览器的控制
- JS对URL字符串进行编码/解码
- avr-gcc的EEPROM的api