자바 압축 패키지 파일 이름 난 장 판 문제 해결
6232 단어 자바
압축 코드
/**
* @param path
* , , .
* @param basePath
* path , new File(path), : zip ,
* null , .
* @param zo
*
* @param isRecursive
*
* @param isOutBlankDir
* , true, baseFile null.
* @throws IOException
*/
public static void zip(String path, File basePath, ZipOutputStream zo,
boolean isRecursive, boolean isOutBlankDir) throws IOException {
File inFile = new File(path);
File[] files = new File[0];
if (inFile.isDirectory()) { //
files = inFile.listFiles();
} else if (inFile.isFile()) { //
files = new File[1];
files[0] = inFile;
}
byte[] buf = new byte[1024];
int len;
// System.out.println("baseFile: "+baseFile.getPath());
for (int i = 0; i < files.length; i++) {
String pathName = "";
if (basePath != null) {
if (basePath.isDirectory()) {
pathName = files[i].getPath().substring(
basePath.getPath().length() + 1);
} else {//
pathName = files[i].getPath().substring(
basePath.getParent().length() + 1);
}
} else {
pathName = files[i].getName();
}
if (files[i].isDirectory()) {
if (isOutBlankDir && basePath != null) {
zo.putNextEntry(new ZipEntry(pathName + "/")); //
}
if (isRecursive) { //
zip(files[i].getPath(), basePath, zo, isRecursive,
isOutBlankDir);
}
} else {
FileInputStream fin = new FileInputStream(files[i]);
zo.putNextEntry(new ZipEntry(pathName));
while ((len = fin.read(buf)) > 0) {
zo.write(buf, 0, len);
}
fin.close();
}
}
inFile = null;
files = null;
}
/**
*
* ZIP
*
* @param directory
*
* @param zipFileName
* ZIP
* @return
*/
public static boolean expZip(String directory, String zipFileName) {
boolean bool = false;
try {
File file = new File(directory);
boolean validate = zipFileName.toLowerCase().endsWith(".zip");
if (!file.exists() || !validate || !file.isDirectory()) {
return false;
} else {
directory = directory.replace(File.separatorChar, '/');
zipFileName = zipFileName.replace(File.separatorChar, '/');
int x = zipFileName.lastIndexOf("/");
if (x <= -1) {
return false;
} else {
String zipdir = zipFileName.substring(0, x);
File zipdirs = new File(zipdir);
zipdirs.mkdirs();
zipdirs = null;
}
OutputStream os = new FileOutputStream(zipFileName);
BufferedOutputStream bs = new BufferedOutputStream(os);
ZipOutputStream zo = new ZipOutputStream(bs);
//[color=red] [/color]
zo.setEncoding(System.getProperty("sun.jnu.encoding"));
ZipOption.zip(directory, new File(directory), zo, true, true);
zo.closeEntry();
zo.close();
bs.close();
os.close();
File exit = new File(zipFileName);
bool = exit.exists();
exit = null;
}
file = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bool;
}
압축 해제 코드
/**
* ZIP
*
* @param zipFileName
* String zip
* @param extPlace
* String
*/
public static void extZipFileList(String zipFileName, String extPlace)
throws Exception {
// ZIP
File fileZip = new File(zipFileName);
boolean exists = fileZip.exists();// /
if (exists) {
// extPlace
File dirs = new File(extPlace);
dirs.mkdirs();
dirs = null;
//
ZipFile zipFile = null;
try {
//[color=red] [/color]
System.setProperty("sun.zip.encoding",System.getProperty("sun.jnu.encoding"));
zipFile = new ZipFile(zipFileName);
FileInputStream zin = new FileInputStream(zipFileName);
ZipInputStream zis = new ZipInputStream(zin);
// jdk ZipEntry
java.util.zip.ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName().replace(
File.separatorChar, '/');
String names[] = entryName.split("/");
int length = names.length;
String path = extPlace;
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += names[v] + "/";
new File(path).mkdir();
} else { //
if (entryName.endsWith("/")) { // ,
new File(extPlace + entryName).mkdir();
} else {
FileOutputStream fos = new FileOutputStream(
extPlace + entryName);
int len;
byte[] buffer = new byte[1024];
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
}
}
zis.closeEntry();
zipEntry = zis.getNextEntry();
}
} catch (Exception e) {
e.printStackTrace();
if (zipFile != null)
zipFile.close();
}
if (zipFile != null)
zipFile.close();
} else {
}
fileZip = null;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.