怎么样解析ZIP(JAVA) JAVA怎么样解压缩ZIP文件.急 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用的是java.util.zip包,我已经实现了下载压缩(里面是XML文件).但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了. 我用的是java.util.zip包,我已经实现了下载压缩(里面是XML文件).但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了. 说的不清楚1、你怎样的压缩下载的(怎么实现的,问题可能在这里)2、server端、client端都是你做的吗 public class Zip { public void zip(InputStream in, OutputStream out) throws IOException { GZIPOutputStream zip = new GZIPOutputStream(out); try { byte[] buffer = new byte[1024]; int bytesRead; while ( (bytesRead = in.read(buffer)) != -1) { zip.write(buffer, 0, bytesRead); } zip.finish(); } finally { if (zip != null) { try { zip.close(); } catch (Throwable e) {} } } } public void zip(File f, File fout) throws IOException { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(f); out = new FileOutputStream(fout); zip(in, out); in.close(); in = null; boolean deleted; try { deleted = f.delete(); } catch (Throwable e) { deleted = false; } if (!deleted) f.deleteOnExit(); } catch (IOException e) { if (out != null) try { out.close(); out = null; } catch (Throwable ex) {} if (fout != null) { boolean deleted; try { deleted = fout.delete(); } catch (Throwable ex) { deleted = false; } if (!deleted) fout.deleteOnExit(); } } finally { if (in != null) try { in.close(); } catch (Throwable e) {} if (out != null) try { out.close(); } catch (Throwable e) {} } } public void zip(File fin, String filename) throws IOException { zip(fin, new File(filename)); } public void zip(String fin, String fout) throws IOException { zip(new File(fin), new File(fout)); } public void zip(String fin, File fout) throws IOException { zip(new File(fin), fout); } public void zip(File f) throws IOException { String path = getDirname(f.getPath()); String name = getBasename(f.getName()); File dir = new File(path); File fout = new File(dir, name + ".zip"); zip(f, fout); } public String getDirname(String path) { char sep = File.separatorChar; int pos1 = path.lastIndexOf(sep); int pos2 = path.lastIndexOf('/'); int pos = pos1; if (pos1 < pos2) pos = pos2; if (pos > 0) return path.substring(0, pos); return "."; } public String getBasename(String path) { char sep = File.separatorChar; int pos1 = path.lastIndexOf(sep); int pos2 = path.lastIndexOf('/'); int pos = pos1; if (pos1 < pos2) pos = pos2; if (pos > 0) return path.substring(pos + 1); return path; } public void zip(String fname) throws IOException { zip(new File(fname)); } public void unzip(InputStream in, OutputStream out) throws IOException { GZIPInputStream unzip = new GZIPInputStream(in); try { byte[] buffer = new byte[1024]; int bytesRead; while ( (bytesRead = unzip.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } finally { if (unzip != null) { try { unzip.close(); } catch (Throwable e) {} } } } public void unzip(File fin, File fout) throws IOException { InputStream in = null; OutputStream out = null; try { in = new FileInputStream(fin); out = new FileOutputStream(fout); unzip(in, out); in.close(); in = null; boolean deleted; try { deleted = fin.delete(); } catch (Throwable e) { deleted = false; } if (!deleted) fin.deleteOnExit(); } catch (IOException e) { if (out != null) try { out.close(); out = null; } catch (Throwable ex) {} if (fout != null) { boolean deleted; try { deleted = fout.delete(); } catch (Throwable ex) { deleted = false; } if (!deleted) fout.deleteOnExit(); } } finally { if (in != null) try { in.close(); } catch (Throwable e) {} if (out != null) try { out.close(); } catch (Throwable e) {} } } public void unzip(File fin, String filename) throws IOException { unzip(fin, new File(filename)); } public void unzip(String fin, String fout) throws IOException { unzip(new File(fin), new File(fout)); } public void unzip(String fin, File fout) throws IOException { unzip(new File(fin), fout); } /** * 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错 * @param f * @throws IOException */ public void unzip(File f) throws IOException { String path = getDirname(f.getPath()); String name = getBasename(f.getName()); int len = name.length(); File dir = new File(path); File fout = new File(dir, name.substring(0, len - 4)); unzip(f, fout); } /** * 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错 * @param fname * @throws IOException */ public void unzip(String fname) throws IOException { unzip(new File(fname)); } /** * 解压含有多个文件的ZIP包 * @param zipFileName * @param extPlace */ private static void extZipFileList(String zipFileName, String extPlace) { try { ZipInputStream in = new ZipInputStream(new FileInputStream( zipFileName)); ZipEntry entry = null; while ( (entry = in.getNextEntry()) != null) { String entryName = entry.getName(); if (entry.isDirectory()) { File file = new File(extPlace + entryName); file.mkdirs(); System.out.println("创建文件夹:" + entryName); } else { FileOutputStream os = new FileOutputStream(extPlace + entryName);// Transfer bytes from the ZIP file to the output file byte[] buf = new byte[1024]; int len; while ( (len = in.read(buf)) > 0) { os.write(buf, 0, len); } os.close(); in.closeEntry(); } } } catch (IOException e) { e.printStackTrace(); } System.out.println("解压文件成功"); } public static void main(String[] args) throws IOException { Zip zip = new Zip(); zip.zip("c:\\111.xls"); zip.extZipFileList("c:\\22.zip","c:\\"); }} java如何将十六进制0xFF5100A5转换为无符号int值,貌似java不支持无符号数据 请大虾们帮我做做面试题。 在一个窗口上如何添加按钮事件? 找些 java 封装 多态 抽象 的练习题 在广州工作的java高手请进来 如何保存文件 请问如何实现中文的拼音或笔划排序 如果将多个程序用到的jar包放到公用jar包中去,让其它jar直接调用 javac dos中输入中文乱码 ,求解,谢谢 JTree是不是有问题?(在JTree中删除一个节点时) [50分赠一人]关于JComboBox的Listener 如何用java解压一个zip或rar的文件
我已经实现了下载压缩(里面是XML文件).
但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....
因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了.
我已经实现了下载压缩(里面是XML文件).
但是压缩完之后java.util.zip把这个XML文件里面的标签和内容顺序给打乱了.有没有办法下载 后并压缩成ZIP文件后,还保持原来的XML顺序和内容....
因为我导入的时候先解压缩这个ZIP文件.但是循序和内容给打乱了.导入就出问题了.
1、你怎样的压缩下载的(怎么实现的,问题可能在这里)
2、server端、client端都是你做的吗
GZIPOutputStream zip = new GZIPOutputStream(out);
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ( (bytesRead = in.read(buffer)) != -1) {
zip.write(buffer, 0, bytesRead);
}
zip.finish();
}
finally {
if (zip != null) {
try {
zip.close();
}
catch (Throwable e) {}
}
}
} public void zip(File f, File fout) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(f);
out = new FileOutputStream(fout);
zip(in, out);
in.close();
in = null;
boolean deleted;
try {
deleted = f.delete();
}
catch (Throwable e) {
deleted = false;
}
if (!deleted)
f.deleteOnExit();
}
catch (IOException e) {
if (out != null)
try {
out.close();
out = null;
}
catch (Throwable ex) {}
if (fout != null) {
boolean deleted;
try {
deleted = fout.delete();
}
catch (Throwable ex) {
deleted = false;
}
if (!deleted)
fout.deleteOnExit();
}
}
finally {
if (in != null)
try {
in.close();
}
catch (Throwable e) {}
if (out != null)
try {
out.close();
}
catch (Throwable e) {}
}
} public void zip(File fin, String filename) throws IOException {
zip(fin, new File(filename));
} public void zip(String fin, String fout) throws IOException {
zip(new File(fin), new File(fout));
} public void zip(String fin, File fout) throws IOException {
zip(new File(fin), fout);
} public void zip(File f) throws IOException {
String path = getDirname(f.getPath());
String name = getBasename(f.getName());
File dir = new File(path);
File fout = new File(dir, name + ".zip");
zip(f, fout);
} public String getDirname(String path) {
char sep = File.separatorChar;
int pos1 = path.lastIndexOf(sep);
int pos2 = path.lastIndexOf('/');
int pos = pos1;
if (pos1 < pos2)
pos = pos2;
if (pos > 0)
return path.substring(0, pos);
return ".";
} public String getBasename(String path) {
char sep = File.separatorChar;
int pos1 = path.lastIndexOf(sep);
int pos2 = path.lastIndexOf('/');
int pos = pos1;
if (pos1 < pos2)
pos = pos2;
if (pos > 0)
return path.substring(pos + 1);
return path;
} public void zip(String fname) throws IOException {
zip(new File(fname));
} public void unzip(InputStream in, OutputStream out) throws IOException {
GZIPInputStream unzip = new GZIPInputStream(in);
try {
byte[] buffer = new byte[1024];
int bytesRead;
while ( (bytesRead = unzip.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
finally {
if (unzip != null) {
try {
unzip.close();
}
catch (Throwable e) {}
}
}
} public void unzip(File fin, File fout) throws IOException {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(fin);
out = new FileOutputStream(fout);
unzip(in, out);
in.close();
in = null;
boolean deleted;
try {
deleted = fin.delete();
}
catch (Throwable e) {
deleted = false;
}
if (!deleted)
fin.deleteOnExit();
}
catch (IOException e) {
if (out != null)
try {
out.close();
out = null;
}
catch (Throwable ex) {}
if (fout != null) {
boolean deleted;
try {
deleted = fout.delete();
}
catch (Throwable ex) {
deleted = false;
}
if (!deleted)
fout.deleteOnExit();
}
}
finally {
if (in != null)
try {
in.close();
}
catch (Throwable e) {}
if (out != null)
try {
out.close();
}
catch (Throwable e) {}
}
} public void unzip(File fin, String filename) throws IOException {
unzip(fin, new File(filename));
} public void unzip(String fin, String fout) throws IOException {
unzip(new File(fin), new File(fout));
} public void unzip(String fin, File fout) throws IOException {
unzip(new File(fin), fout);
} /**
* 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
* @param f
* @throws IOException
*/
public void unzip(File f) throws IOException {
String path = getDirname(f.getPath());
String name = getBasename(f.getName());
int len = name.length();
File dir = new File(path);
File fout = new File(dir, name.substring(0, len - 4));
unzip(f, fout);
} /**
* 解压单个文件的压缩包,请注意如果压缩包有多个文件,不会成功也不会报错
* @param fname
* @throws IOException
*/
public void unzip(String fname) throws IOException {
unzip(new File(fname));
} /**
* 解压含有多个文件的ZIP包
* @param zipFileName
* @param extPlace
*/
private static void extZipFileList(String zipFileName, String extPlace) {
try { ZipInputStream in = new ZipInputStream(new FileInputStream(
zipFileName)); ZipEntry entry = null; while ( (entry = in.getNextEntry()) != null) { String entryName = entry.getName(); if (entry.isDirectory()) {
File file = new File(extPlace + entryName);
file.mkdirs();
System.out.println("创建文件夹:" + entryName);
}
else { FileOutputStream os = new FileOutputStream(extPlace
+ entryName);// Transfer bytes from the ZIP file to the output file
byte[] buf = new byte[1024]; int len;
while ( (len = in.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
in.closeEntry(); }
} }
catch (IOException e) {
e.printStackTrace();
}
System.out.println("解压文件成功");
} public static void main(String[] args) throws IOException {
Zip zip = new Zip();
zip.zip("c:\\111.xls");
zip.extZipFileList("c:\\22.zip","c:\\");
}
}