📄 jarutil.java
字号:
package javazip.util;
import java.util.ArrayList;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.JarInputStream;
import javazip.base.FileInfo;
/**
* 实现jar文件压缩和解压缩的类
*
* @author dragon
*/
public class JarUtil extends CompressUtil {
final int BUF_SIZE = 1024 * 64;
/**
* jar文件的压缩的方法
*
* @see javazip.util.CompressUtil#compressFile(java.util.ArrayList)
*/
public void compressFile(ArrayList fileList, String target) {
File[] list = new File[fileList.size()];
for (int i = 0; i < fileList.size(); i++) {
list[i] = (File) fileList.get(i);
}
JarOutputStream jos = null;
File file = null;
try {
if (list == null || list.length < 1)
return;
file = (File) list[0];
if (!file.exists() || !file.canRead())
return;
String curPath = file.getPath().substring(0,
file.getPath().length() - file.getName().length() - 1);
jos = new JarOutputStream(new FileOutputStream(target)); // overwrite
for (int i = 0; i < list.length; i++)
if (!jarDir(jos, curPath, ((File) list[i]).toString()))
return;
jos.close();
return;
} catch (Exception e) {
return;
}
}
/**
*
*/
public void compressFile(File list[], String target) {
// File [] list=new File[fileList.size()];
// for(int i=0;i<fileList.size();i++)
// {
// list[i]=(File)fileList.get(i);
// }
JarOutputStream jos = null;
File file = null;
try {
if (list == null || list.length < 1)
return;
file = (File) list[0];
if (!file.exists() || !file.canRead())
return;
String curPath = file.getPath().substring(0,
file.getPath().length() - file.getName().length() - 1);
jos = new JarOutputStream(new FileOutputStream(target)); // overwrite
for (int i = 0; i < list.length; i++)
if (!jarDir(jos, curPath, ((File) list[i]).toString()))
return;
jos.close();
return;
} catch (Exception e) {
return;
}
}
/**
*
* @param jos
* @param curPath
* @param srcDir
* @return
*/
private boolean jarDir(JarOutputStream jos, String curPath, String srcDir) {
try {
File file = new File(srcDir);
File[] files = file.listFiles();
if (files == null) {
if (!file.isDirectory())
doJar(jos, curPath, file.getPath());
} else
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()
&& !files[i].getName().equals(".")
&& !files[i].getName().equals(".."))
jarDir(jos, curPath, files[i].getPath());
else
doJar(jos, curPath, files[i].getPath());
}
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* @param jos
* @param curPath
* @param fullFileName
* @return
*/
private boolean doJar(JarOutputStream jos, String curPath,
String fullFileName) {
try {
File file = new File(fullFileName);
if (!file.isDirectory()) {
jos.putNextEntry(new ZipEntry(new String(fullFileName
.substring(curPath.length()))));
FileInputStream fis = new FileInputStream(file);
int readSize = 0;
byte[] buf = new byte[BUF_SIZE];
while ((readSize = fis.read(buf, 0, BUF_SIZE)) > 0) {
jos.write(buf, 0, readSize);
}
fis.close();
fis = null;
}
jos.flush();
return true;
} catch (Exception e) {
return false;
}
}
/**
* jar文件解压缩的方法(还有点问题)
*
* @see javazip.util.CompressUtil#extractFile(java.lang.String)
*/
public void extractFile(String source, String target) {
/*
* try { FileInputStream fis = new FileInputStream(source);
* JarInputStream jis = new JarInputStream(fis); ZipEntry ze = null;
*
* File file = new File(target); if (!target.endsWith("\\")) target =
* target.concat("\\");
*
* if (!file.exists() && !file.mkdir()) { System.out.println("Cannot
* make dir."); }
*
* FileOutputStream fos = null; byte[] buf = new byte[BUF_SIZE]; int
* readSize = 0; File dir = null; String dirName; while ((ze =
* jis.getNextJarEntry()) != null) { dirName = target + ze.getName();
* file = new File(target + ze.getName()); System.out.println(target +
* ze.getName()); dirName = dirName.substring(0, dirName.length() -
* file.getName().length() - 1); System.out.println(dirName); dir = new
* File(dirName); if (!dir.exists() && !dir.mkdirs()) {
* System.out.println("Cannot make dir."); return; }
*
* if (ze.isDirectory()) { System.out.println("*** DIR ***"); if
* (!file.mkdir()) return; } else { file.createNewFile(); fos = new
* FileOutputStream(file); while ((readSize = jis.read(buf, 0,
* BUF_SIZE)) > 0) fos.write(buf, 0, readSize); fos.close(); } }
*
* jis.close(); fis.close(); return; } catch (Exception e) { return; }
*/
JarInputStream in = null;
BufferedOutputStream out = null;
File targetFile = new File(target);
int count = 0;
try {
in = new JarInputStream(new BufferedInputStream(
new FileInputStream(source)));
JarEntry jarEntry;
while ((jarEntry = in.getNextJarEntry()) != null) {
//debug begin{
System.out.println("\n\njarEntry.getName()="
+ jarEntry.getName());
//}debug end
String jarEntryName = jarEntry.getName();
//将ZipEntry中的标准分割符'/'换成本机分隔符
jarEntryName = jarEntryName.replace('/', '\\');
if (jarEntryName.endsWith("\\")) //以'\'结尾说明是文件夹
{
continue;
}
File outputFile = new File(targetFile.getAbsolutePath() + "\\"
+ jarEntryName.substring(jarEntryName.indexOf("\\")));
System.out.println("outputFile.getAbsolutePath()="
+ outputFile.getAbsolutePath());
if (!outputFile.exists()) {
new File(outputFile.getParent()).mkdirs();
}
out = new BufferedOutputStream(new FileOutputStream(outputFile));
int data;
while ((data = in.read()) != -1) {
out.write(data);
}
out.close();
}
in.close();
} catch (Exception e) {
e.printStackTrace();
// JOptionPane.showMessageDialog(null,e.toString());
}
}
/**
*
*/
public Long getOriginalLength(File f) {
long result = 0;
try {
ZipEntry zipEntry = null;
JarInputStream zis = new JarInputStream(new FileInputStream(f));
if (!f.exists()) {
return null;
} else {
while ((zipEntry = zis.getNextEntry()) != null) {
result += zipEntry.getSize();
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return new Long(result);
}
/**
*
*/
public boolean testFile(File f) {
int count = 0;
try {
ZipEntry zipEntry = null;
JarInputStream zis = new JarInputStream(new FileInputStream(f));
if (!f.exists()) {
return false;
} else {
while ((zipEntry = zis.getNextEntry()) != null) {
count++;
}
}
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
if (count > 0)
return true;
else
return false;
}
public static void main(String args[]) {
JarUtil u = new JarUtil();
System.out.println(u.testFile(new File("F:\\dt.jar")));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -