java 写的压缩、解压的代码 写的压缩、 import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class FileMgr { public FileMgr() { } /** * 压缩文件 * @param srcfile File[] 需要压缩的文件
列表 * @param zipfile File */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); // Compress the files for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(srcfile[i].getName())); // Transfer bytes from the file to the ZIP file int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); 压缩后的文件
} // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 解
压缩 * @param zipfile File 需要解压缩的文件 * @param descDir String 解压后的目标目录 */ public static void UnZipFiles(java.io.File zipfile, String descDir) { try { // Open the ZIP file ZipFile zf = new ZipFile(zipfile); for (Enumeration entries = zf.entries(); entries.hasMoreElements(); ) { // Get the entry name ZipEntry entry = ( (ZipEntry) entries.nextElement()); String zipEntryName = entry.getName(); InputStream in = zf.getInputStream(entry); // System.out.println(zipEntryName); OutputStream out = new FileOutputStream(descDir + zipEntryName); byte[] buf1 = new byte[1024];
int len; while ( (len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } // Close the file and stream in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
java 解压缩的中文问题的解决 解压缩的中文问题的解决. 与到中文的
问题都是 java 编程常遇到的问题,当我在做个解压缩的部分时又遇到它了.我刚开始 还有点蒙!到后在网上查了一些资料后还是把他给解决了.我现在把它发布到网上,方便有需要的 朋友作个参考!我是用 ant.jar 包来解决的,当然也实现了中文文件名的处理.程序处理了文件夹 的嵌套问题和解同一个文件夹下的所有.zip 文件.这里
程序只能解.zip 的文件.其他的格式是不 支持的; import org.apache.tools.zip.*; import java.io.*; //import java.util.*; /** * this class used to unzip ,only being fit to zip files * @author wuzhaoxue * */ public class Unzip { /** * 该方法处理目录嵌套
* @param directory * @param subDirectory */ private void getDir(String directory, String subDirectory){ String dir[]; File fileDir = new File(directory); try { if (subDirectory == "" &;&; fileDir.exists() != true) fileDir.mkdir(); else if (subDirectory != "") { dir = subDirectory.replace('\\', '/').split("/"); for (int i = 0; i < dir.length; i++) { File subFile = new File(directory + File.s
eparator + dir[i]); if (subFile.exists() == false) subFile.mkdir(); directory += File.separator + dir[i]; } } }catch (Exception ex) { System.out.println(ex.getMessage()); } } /** * * @param zipFileNaame * @param outputDirectory * */ public void unZip(String zipFileName, String outputDirectory){ try { ZipFile zipFile = new ZipFile(zipFileName); java.util.Enumeration e = zipFile.getEntries(); ZipEntry zipEntry = null; getDir(outputDirectory, ""); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); //System.o