import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipInputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class ZipUtil
{
static void createDir(String path)
{
File dir = new File(path);
if (dir.exists() == false)
dir.mkdir();
}
static String getSuffixName(String name)
{
return name.substring(0, name.lastIndexOf("."));
}
public static void unzip(File src, File dstDir) throws IOException
{
ZipFile zipFile = null;
try
{
// 创建zip文件对象
//zipFile = new ZipFile(src);
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0){
zipFile = new ZipFile(src,"GBK");
}else if(System.getProperty("os.name").toLowerCase().indexOf("linux") >= 0){
//zipFile = new ZipFile(src,"GB2312");
zipFile = new ZipFile(src,"GBK");
}
// 得到zip文件条目枚举对象
Enumeration zipEnum = zipFile.getEntries();
// 定义输入输出流对象
// 定义对象
ZipEntry entry = null;
// 循环读取条目
while (zipEnum.hasMoreElements())
{
// 得到当前条目
entry = (ZipEntry) zipEnum.nextElement();
String entryName = new String(entry.getName());
// 用/分隔条目名称
String names[] = entryName.split("\\/");
int length = names.length;
String path = dstDir.getAbsolutePath();
for (int v = 0; v < length; v++)
{
if (v < length - 1)
{ // 最后一个目录之前的目录
path += "/" + names[v] + "/";
createDir(path);
}
else
{ // 最后一个
if (entryName.endsWith("/")) // 为目录,则创建文件夹
createDir(dstDir.getAbsolutePath() + "/" + entryName);
else
{
InputStream input = null;
OutputStream output = null;
try
{// 为文件,则输出到文件
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(dstDir.getAbsolutePath() + "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
// 关闭流
output.flush();
}
finally
{
if (input != null)
input.close();
if (output != null)
output.close();
}
}
}
}
}
}
finally
{
if (zipFile != null)
zipFile.close();
}
}
public static void unzip(String zipFilePath, String unzipDirectory) throws Exception
{
// 创建文件对象
File file = new File(zipFilePath);
// 创建本zip文件解压目录
File unzipFile = new File(unzipDirectory);
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdir();
unzip(file, unzipFile);
}
public static byte[] unZip(byte[] data)
{
byte[] b = null;
try
{
ByteArrayInputStream bis = new ByteArrayInputStream(data);
ZipInputStream zip = new ZipInputStream(bis);
while (zip.getNextEntry() != null)
{
byte[] buf = new byte[1024];
int num = -1;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((num = zip.read(buf, 0, buf.length)) != -1)
{
baos.write(buf, 0, num);
}
b = baos.toByteArray();
baos.flush();
baos.close();
}
zip.close();
bis.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return b;
}
public static byte[] gZip(byte[] data) {
byte[] b = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(bos);
gzip.write(data);
gzip.finish();
gzip.close();
b = bos.toByteArray();
bos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return b;
}
public static void main(String[] args) throws Exception
{
// unzip("F:/aaaaaaaa.zip", "F:");
// System.out.println("over....................");
String a = "hellp aa bb cc";
System.out.println(a.lastIndexOf("he"));
}
}
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!