/// 压缩程度,范围0-9,数值越大,压缩程序越高 /// 分块大小 public void Zip File (string file ToZip, string ziped File , int compressionLevel, int blockSize) if (!System.IO. File .Exists( file ToZip))//如果文件没有找到,则报错 throw new File NotFoundException("The specified file " + file ToZip + " could not be found. Zipping aborderd"); File Stream streamToZip = new File Stream( file ToZip, File Mode.Open, File Access.Read); File Stream zip File = File . Create (ziped File ); ZipOutputStream zipStream = new ZipOutputStream(zip File ); ZipEntry zipEntry = new ZipEntry( file ToZip); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int size = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, size); while (size < streamToZip.Length) int sizeRead = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sizeRead); size += sizeRead; catch (Exception ex) GC.Collect(); throw ex; zipStream.Finish(); zipStream.Close(); streamToZip.Close(); GC.Collect(); /// 压缩目录(包括子目录及所有文件) /// 要压缩的根目录 /// 保存 路径 /// 压缩程度,范围0-9,数值越大,压缩程序越高 public void Zip File FromDirectory(string rootPath, string destinationPath, int compressLevel) GetAllDirectories(rootPath); /* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查 路径 是否以"\"结尾 rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\" //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前 路径 的位置,以备压缩时将所压缩内容转变成相对 路径 。 string rootMark = rootPath + "\\";//得到当前 路径 的位置,以备压缩时将所压缩内容转变成相对 路径 。 Crc32 crc = new Crc32(); ZipOutputStream outPutStream = new ZipOutputStream( File . Create (destinationPath)); outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression foreach (string file in file s) File Stream file Stream = File .OpenRead( file );//打开压缩文件 byte[] buffer = new byte[ file Stream.Length]; file Stream.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry( file .Replace(rootMark, string.Empty)); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip file s that don't store // the size and crc in the header. entry.Size = file Stream.Length; file Stream.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; outPutStream.PutNextEntry(entry); outPutStream.Write(buffer, 0, buffer.Length); this. file s.Clear(); foreach (string emptyPath in paths) ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/"); outPutStream.PutNextEntry(entry); this.paths.Clear(); outPutStream.Finish(); outPutStream.Close(); GC.Collect(); /// 取得目录下所有文件及文件夹,分别存入 file s及paths /// 根目录 private void GetAllDirectories(string rootPath) string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录 foreach (string path in subPaths) GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List string[] file s = Directory.Get File s(rootPath); foreach (string file in file s) this. file s.Add( file );//将当前目录中的所有文件全名存入文件List if (subPaths.Length == file s.Length && file s.Length == 0)//如果是空目录 this.paths.Add(rootPath);//记录空目录 /// 解压缩文件(压缩文件中含有子目录) /// 待解压缩的文件 路径 /// 解压缩到指定目录 /// 解压后的文件列表 public List UnZip(string zipfilepath , string unzippath) //解压出来的文件列表 List unzip File s = new List(); //检查输出目录是否以“\\”结尾 if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false) unzippath += "\\"; ZipInputStream s = new ZipInputStream( File .OpenRead( zipfilepath )); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) string directoryName = Path.GetDirectoryName(unzippath); string file Name = Path.Get File Name(theEntry.Name); //生成解压目录【用户解压到硬盘根目录时,不需要创建】 if (!string.IsNullOrEmpty(directoryName)) Directory. Create Directory(directoryName); if ( file Name != String.Empty) //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入 if (theEntry.CompressedSize == 0) break; //解压文件到指定的目录 directoryName = Path.GetDirectoryName(unzippath + theEntry.Name); //建立下面的目录和子目录 Directory. Create Directory(directoryName); //记录导出的文件 unzip File s.Add(unzippath + theEntry.Name); File Stream streamWriter = File . Create (unzippath + theEntry.Name); int size = 2048; byte[] data = new byte[2048]; while (true) size = s.Read(data, 0, data.Length); if (size > 0) streamWriter.Write(data, 0, size); break; streamWriter.Close(); s.Close(); GC.Collect(); return unzip File s; import java.util.zip.Adler32; import java.util.zip.CheckedInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipUtils { private static final int BUFFER = 8192; private static void log(String msg){ System.out.println (msg); private static String get File Name(String file Path){ int index = file Path.indexOf("."); return file Path.substring(0, index); @SuppressWarnings("unused") private static String getRootPath(String file Path){ int index = file Path.indexOf("."); return file Path.substring(0, index); public static void zip(String source File Path){ File file Dir = new File (source File Path); if( file Dir.exists()){ log( file Dir.getPath()+" Starting Zip ..."); long startTime = System.currentTimeMillis(); doZip( file Dir); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; log("Zip Success!"); log("use time -- "+costTime+" millsec!"); }else{ log("can't find the File !"); public static void unZip(String zipFilePath ){ File file Dir = new File ( zipFilePath ); if( file Dir.exists()){ log( file Dir.getPath()+" Starting UnZip ..."); long startTime = System.currentTimeMillis(); doUnZip( file Dir); long endTime = System.currentTimeMillis(); long costTime = endTime - startTime; log("UnZip Success!"); log("use time -- "+costTime+" millsec!"); }else{ log("can't find the File !"); public static void doZip( File file ){ List< File > file List = new ArrayList< File >(); List< File > all File s = (ArrayList< File >)search File s( file .getPath(), file List); Object[] file Array = all File s.toArray(); BufferedInputStream in = null; File InputStream fis = null; ZipOutputStream zos = null; File OutputStream fos = null; try { fos = new File OutputStream( file .getParent()+ File .separator+ file .getName()+".zip"); zos = new ZipOutputStream(new BufferedOutputStream(fos, BUFFER)); zos.setLevel(9); byte[] data = new byte [BUFFER]; for (int i = 0; i< file Array.length; i++){ // 设置压缩文件入口entry,为被读取的文件创建压缩条目 File temp File = new File ( file Array[i].toString()); String rootStr = file .getPath(); String entryStr = null; // entry以相对 路径 的形式设置。以文件夹C:\temp例如temp\test.doc或者test.xls 如果设置不当,会出现 拒绝 访问 等错误 // 分别处理单个文件/目录的entry if(rootStr.equals(temp File .getPath())){ entryStr = temp File .getName(); }else{ entryStr = temp File .getPath().substring((rootStr+ File .separator).length()); log(entryStr); ZipEntry entry = new ZipEntry(entryStr); zos.putNextEntry(entry); fis = new File InputStream(temp File ); in = new BufferedInputStream(fis, BUFFER); int count; while((count = in.read(data, 0, BUFFER)) != -1){ zos.write(data, 0, count); catch (Exception ex) { ex.printStackTrace(); }finally{ if(in != null){ in.close(); if(zos != null){ zos.close(); }catch (Exception e) { e.printStackTrace(); public static void doUnZip( File file ){ final int BUFFER = 2048; BufferedOutputStream dest = null; File InputStream fis = new File InputStream( file ); CheckedInputStream checksum = new CheckedInputStream(fis,new Adler32()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum)); ZipEntry entry; while((entry = zis.getNextEntry()) != null){ log("Extracting: "+entry); int count; byte[] data = new byte[BUFFER]; log("unzip to "+get File Name( file .getPath())); File OutputStream fos = new File OutputStream(get File Name( file .getPath())+ File .separator+newDir( file , entry.getName())); dest = new BufferedOutputStream(fos, BUFFER); while((count = zis.read(data, 0, BUFFER)) != -1){ dest.write(data, 0, count); dest.flush(); dest.close(); zis.close(); System.out.println("Checksum: "+checksum.getChecksum().getValue()); }catch(Exception e){ e.printStackTrace(); public static List< File > search File s(String source File Path, List< File > file List){ File file Dir = new File (source File Path); if( file Dir.isDirectory()){ File [] sub file s = file Dir.list File s(); for(int i = 0; i<sub file s.length; i++){ search File s(sub file s[i].getPath(), file List); }else{ file List.add( file Dir); return file List; @SuppressWarnings("static-access") private static String newDir( File file ,String entryName) { String rootDir=get File Name( file .getPath()); log("root:"+rootDir); int index = entryName.lastIndexOf("\\"); String dirStr=new File (rootDir).getParent(); log(dirStr); if (index != -1) { String path=entryName.substring(0, index); log("new Dir:"+rootDir+ file .separator+path); new File (rootDir+ file .separator+path).mkdirs(); log("entry:"+entryName.substring(0, index)); else{ new File (rootDir).mkdirs(); log("entry:"+entryName); return entryName; import java.io. File ; import java.io. File InputStream; import java.io. File NotFoundException; import java.io. File OutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.Zip File ; import org.apache.tools.zip.ZipOutputStream; * 类名: ZipUtil.java * 描述:压缩/解压缩zip包处理类 * 创建者: XX X * 创建日期:2015年5月7日 - 下午1:35:02 * 版本: V0.1 * 修改者: * 修改日期: public class ZipUtil { * 功能描述:压缩文件 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:35:18 * 版本: V0.1 * 修改者: * 修改日期: * @param directory 指定压缩文件 路径 压缩到同目录 * @throws IOException * void public static void zip(String directory) throws File NotFoundException, IOException { zip("", null, directory); * 功能描述:压缩文件 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:36:03 * 版本: V0.1 * 修改者: * 修改日期: * @param zip File Name 压缩产生的zip包文件名--带 路径 ,如果为null或空则默认按文件名生产压缩文件名 * @param relativePath 相对 路径 ,默认为空 * @param directory 文件或目录的绝对 路径 * void public static void zip(String zip File Name, String relativePath, String directory) throws File NotFoundException, IOException { String file Name = zip File Name; if ( file Name == null || file Name.trim().equals("")) { File temp = new File (directory); if (temp.isDirectory()) { file Name = directory + ".zip"; } else { if (directory.indexOf(".") > 0) { file Name = directory.substring(0, directory.lastIndexOf("."))+ "zip"; } else { file Name = directory + ".zip"; ZipOutputStream zos = new ZipOutputStream(new File OutputStream( file Name)); try { zip(zos, relativePath, directory); } catch (IOException ex) { throw ex; } finally { if (null != zos) { zos.close(); * 功能描述:压缩文件 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:37:55 * 版本: V0.1 * 修改者: * 修改日期: * @param zos 压缩输出流 * @param relativePath 相对 路径 * @param absolutPath 文件或文件夹绝对 路径 * @throws IOException * void private static void zip(ZipOutputStream zos, String relativePath, String absolutPath) throws IOException { File file = new File (absolutPath); if ( file .isDirectory()) { File [] file s = file .list File s(); for (int i = 0; i < file s.length; i++) { File temp File = file s[i]; if (temp File .isDirectory()) { String newRelativePath = relativePath + temp File .getName() + File .separator; create ZipNode(zos, newRelativePath); zip(zos, newRelativePath, temp File .getPath()); } else { zip File (zos, temp File , relativePath); } else { zip File (zos, file , relativePath); * 功能描述:压缩文件 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:38:46 * 版本: V0.1 * 修改者: * 修改日期: * @param zos 压缩输出流 * @param file 文件对象 * @param relativePath 相对 路径 * @throws IOException * void private static void zip File (ZipOutputStream zos, File file , String relativePath) throws IOException { ZipEntry entry = new ZipEntry(relativePath + file .getName()); zos.putNextEntry(entry); InputStream is = null; try { is = new File InputStream( file ); int BUFFERSIZE = 2 <= 0) { zos.write(buffer, 0, length); zos.flush(); zos.closeEntry(); } catch (IOException ex) { throw ex; } finally { if (null != is) { is.close(); * 功能描述:创建目录 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:39:12 * 版本: V0.1 * 修改者: * 修改日期: * @param zos zip输出流 * @param relativePath 相对 路径 * @throws IOException * void private static void create ZipNode(ZipOutputStream zos, String relativePath) throws IOException { ZipEntry zipEntry = new ZipEntry(relativePath); zos.putNextEntry(zipEntry); zos.closeEntry(); * 功能描述:解压缩文件 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:39:32 * 版本: V0.1 * 修改者: * 修改日期: * @param zipFilePath zip文件 路径 * @param targetPath 解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下 * void public static void unzip(String zipFilePath , String targetPath) throws IOException { InputStream is = null; File OutputStream file Out = null; File file = null; Zip File zip File = null; try { zip File = new Zip File ( zipFilePath ,"GBK"); String directoryPath = ""; if (null == targetPath || "".equals(targetPath)) { directoryPath = zipFilePath .substring(0, zipFilePath .lastIndexOf(".")); } else { directoryPath = targetPath; for(Enumeration entries = zip File .getEntries(); entries.hasMoreElements();){ ZipEntry entry = (ZipEntry)entries.nextElement(); file = new File (directoryPath+"/"+entry.getName()); if(entry.isDirectory()){ file .mkdirs(); }else{ //如果指定文件的目录不存在,则创建之. File parent = file .getParent File (); if(!parent.exists()){ parent.mkdirs(); is = zip File .getInputStream(entry); file Out = new File OutputStream( file ); int readLen = 0; byte[] buffer = new byte[4096]; while ((readLen = is.read(buffer, 0, 4096)) >= 0) { file Out.write(buffer, 0, readLen); file Out.close(); is.close(); zip File .close(); } catch (IOException ex) { throw ex; } finally { if(null != zip File ){ zip File = null; if (null != is) { is.close(); if (null != file Out) { file Out.close(); * 功能描述:生产文件 如果文件所在 路径 不存在则生成 路径 * 创建者: XX X * 创建日期: 2015年5月7日 - 下午1:41:04 * 版本: V0.1 * 修改者: * 修改日期: * @param file Name 文件名 带 路径 * @param isDirectory 是否为 路径 * @return * File public static File build File (String file Name, boolean isDirectory) { File target = new File ( file Name); if (isDirectory){ target.mkdirs(); } else { if (!target.getParent File ().exists()) { target.getParent File ().mkdirs(); target = new File (target.getAbsolutePath()); return target;
Ant是Java程序员的一个好的工具,主要可以帮助程序员进行java项目的的管理,包括批量编译、部署、文档生成等工作,其用途远不止如此,ant内置了大量的API进行各种文件系统操作,在各种应用服务器中都被广泛应用于程序和资源的部署。 Ant功能强大的地方在于,程序员不仅能通过编写Ant的脚本(build.xml)来进行各种文件部署管理操作,还可以通过调用Ant的丰富的API,甚至扩展Ant的API进行编程。 1. 目录操作: 1) 创建目录 1. Project prj=new Project(); 2. Mkdir mkdir=new Mkdir(); 3. mkdir.setProject(prj); 4. mkdir.setDir(new File ("d:tempdir1")); 5. mkdir.execute(); 2) 删除目录 1. Project prj=new Project(); 2. Delete delete=new Delete(); 3. delete.setProject(prj); 4. delete.setDir(new File ("d:tempdir1")); //可同时将子目录及所有文件删除 5. delete.execute(); 注:对每一个Ant Task,如Mkdir,Delete、Copy、Move、Zip等,都必须设置一个Project对象,可以几个Ant Task共用一个Project对象,但不能有Ant Task不设置Project对象。 2. 文件拷贝和移动、更名 1)文件copy 1. Project prj=new Project(); 2. Copy copy=new Copy(); 3. copy.setProject(prj); 4. copy.set File (new File ("d:tempf1.txt"); 5. copy.setTodir(new File ("d:tempdir1")); 6. copy.execute(); //将f1.txt文件copy到dir1中 2)copy文件并同时替换其中的内容, 如将 xml中的 @eosapp_name@ 替换成真正的应用名称 1. Project prj=new Project(); 2. Copy copy = new Copy(); 3. copy.setEncoding("UTF-8"); 4. copy.setProject(prj); 5. copy.setTodir("d:temp"); 7. File Set file Set=new File Set(); 8. file Set.setDir(new File (eosHome "/base/template.app")); 9. file Set.setIncludes("**/*.xml"); 10. copy.add File set( file Set); 12. FilterSet filter=copy. create FilterSet(); 13. filter.addFilter("eosapp_name","app1"); 14. copy.execute(); 2)文件或目录移动 Move的用法和Copy用法基本一致,Move本身为Copy的子类。 1. Project prj=new Project(); 2. Copy copy=new Copy(); 3. copy.setProject(prj); 4. copy.set File (new File ("d:tempf1.txt"); 5. copy.setTodir(new File ("d:tempdir1")); 6. copy.execute(); //将f1.txt文件移动到dir1中 3)文件改名: 1. Project prj=new Project(); 2. Copy copy=new Copy(); 3. copy.setProject(prj); 4. copy.set File (new File ("d:tempf1.txt"); 5. copy.setTodir(new File ("d:tempf2.txt")); 6. copy.execute(); //将f1.txt文件更名为f2.txt中 4)目录更名: 1. Project prj=new Project(); 2. Copy copy=new Copy(); 3. copy.setProject(prj); 4. copy.set File (new File ("d:tempdir1"); 5. copy.setTodir(new File ("d:tempdir2")); 6. copy.execute(); //将dir1目录更名为dir2,相当于将dir1目录下的所有文件移到dir2目录下 3.使用文件集 File Set 使用文件集可以同时将多个满足匹配条件的文件集合进行copy、move和压缩等操作。 1. Project prj=new Project(); 2. Copy copy=new Copy(); 3. copy.setProject(prj); 4. copy.setTodir(new File ("d:temptodir")); 6. File Set fs=new File Set(); 7. fs.setProject(prj); 8. fs.setDir(new File ("d:javaprjsrc")); 9. fs.setIncludes("**/*.*"); //包含所有文件 10. fs.setExcludes("**/CVS,**/*.class"); //排除CVS相关文件,以及.class文件 11. copy.add File set(fs); 13. copy.execute(); 注: File Set的setIncludes, 和setExcludes方法输入pattern, pattern是一个使用“,”或空格分隔的匹配字符串,其中, “**”代表所有文件或目录,“*.*”代表说有文件, “*.java”代表所有扩展名为java的文件。 4.目录扫描,查找文件 1. DirectoryScanner ds=new DirectoryScanner(); 2. ds.setBasedir(new File ("d:tempwar")); 3. ds.setIncludes(new String[] ); 4. ds.scan(); 5. if(ds.getIncluded File sCount()>0) { 6. System.out.println("found jsp!"); 7. String[] include File s=ds.getIncluded File s(); 8. for(String file :include File s){ 9. System.out.println( file ); 10. } 11. } 5.文件压缩,打包 //压缩为zip文件 1. Project prj=new Project(); 2. Zip zip=new Zip(); 3. zip.setProject(prj); 4. zip.setDest File (new File ("d:tempsrc.zip")); 5. File Set file Set=new File Set(); 6. file Set.setProject(prj); 7. file Set.setDir(new File ("d:javaprjprj1src")); 8. file Set.setIncludes("**/*.java"); 9. zip.add File set( file Set); 10. zip.execute(); 12. //将class文件打成jar包 13. Project prj=new Project(); 14. Jar jar=new Jar(); 15. jar.setProject(prj); 16. jar.setDest File (new File ("d:tempprj1.jar")); 17. File Set file Set=new File Set(); 18. file Set.setProject(prj); 19. file Set.setDir(new File ("d:javaprjprj1bin")); 20. file Set.setIncludes("**/*.class,**/*.properties"); 21. jar.add File set( file Set); 22. jar.execute(); 6.文件解压 1)将压缩文件中的所有文件解压 1. Project prj=new Project(); 2. Expand expand=new Expand(); 3. expand.setProject(prj); 4. expand.setSrc(new File ("d:tempsrc.zip")); 5. expand.setOverwrite(overwrite); 6. expand.setDest("d:tempoutsrc"); 7. expand.execute(); 2)将压缩文件中的符合匹配条件的文件解压 1. Project prj=new Project(); 2. Expand expand=new Expand(); 3. expand.setProject(prj); 4. expand.setSrc(new File ("d:tempsrc.zip")); 5. expand.setOverwrite(overwrite); 6. expand.setDest("d:tempoutsrc"); 7. PatternSet patternset = new PatternSet(); 8. patternset.setIncludes("**/*.java"); 9. patternset.setProject(prj); 10. expand.addPatternset(patternset); 11. expand.execute(); 3)利用Mapper解压文件: 如将 .../lib/*.jar 解压到 .../WEB-INF/lib目录下(去除目录结构) 1. Expand expand = new Expand(); 2. expand.setProject(prj); 3. expand.setSrc(new File ( zipFilePath )); 4. expand.setDest(new File (webDir "/WEB-INF/lib")); 6. PatternSet pattern = new PatternSet(); 7. pattern.setIncludes("lib/*.jar"); 8. expand.addPatternset(pattern); 10. File NameMapper mapper=new Flat File NameMapper(); 11. expand.add(mapper); 13. /* another way using mapper 14. Mapper mapper=expand. create Mapper(); 15. MapperType type=new MapperType(); 16. type.setValue("flatten"); 17. mapper.setType(type); 18. */ 19. expand.execute(); 7.读取zip文件 1) 读取zip文件中的文件和目录 1. Zip File zip file = new Zip File (new File ( file path)); 2. for (Enumeration entries = zip file .getEntries(); entries.hasMoreElements();) { 3. ZipEntry entry = (ZipEntry) entries.nextElement(); 4. if(entry.isDirectory()) 5. System.out.println("Directory: " entry.getName()); 6. else 7. System.out.println(" file : " entry.getName()); 9. zip file .close(); //Zip File 用完必须close,否则文件被锁定 2)zip文件扫描,在Zip文件中查找目录或文件 1. ZipScanner scan=new ZipScanner(); 2. scan.setSrc(new File ("d:temptest.zip")); 3. scan.setIncludes(new String[] ); //查找目录(一、二级目录); 4. scan.scan(); 5. String dirs[]=scan.getIncludedDirectories(); 6. scan.setIncludes(new String[]); //查找文件 7. scan.scan(); 8. String file s[]=scan.getIncluded File s(); 从之前发布其他chm文件下载用户的反映看,有不少朋友反映下载后打开无法显示,这一般不是chm文件的问题,这里统一说明一下解决办法: 如果文件打开看不到右边的内容,是因为你的操作系统为了安全对下载的chm文件进行了锁定,只需要在打开前右键单击该chm文件选择“属性”,然后在“常规”选项卡的下方单击“解除锁定”按钮就可以了。如果还是不能看,请再查看一下你的chm文件所存储的目录或文件名是否有特殊字符如“#”号字符等,去掉特殊字符即可。
今天写wince程序 由于SD卡中文件检查如果没有则从\\windows目录下拷贝到SD卡中回复原始文件使用,结果在打开文件时总是 Create File 拒绝 访问 。 检查了N边 路径 什么全都没错连SD卡分区都换成FAT32及FAT也不好使。着了半天原因最后发现 考来的文件是只读属性因此在打开修改时就会 拒绝 访问 。最后  解决方法:将“只读”属性去掉。
public class Count File sInZip { public static void main(String[] args) { String zipFilePath = "path/to/your/zip/ file .zip"; int file Count = 0; try (ZipInputStream zis = new ZipInputStream(new File InputStream( zipFilePath ))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.isDirectory()) { file Count++; System.out.println("Number of file s in the zip: " + file Count); } catch (IOException e) { e.printStackTrace(); }解释一下这段代码
这段代码是一个Java类,名为Count File sInZip,其中包含一个main()方法。它的目的是计算一个压缩文件中的文件数量。 首先,我们定义了一个字符串变量" zipFilePath ",用于存储要处理的压缩文件的 路径 。请将其替换为您的实际 路径 。 然后,我们声明了一个整型变量" file Count",用于计算文件数量,并初始化为0。 接下来,我们使用try-with-resources语句创建了一个ZipInputStream对象。它接受一个 File InputStream对象作为参数,该对象用于读取压缩文件。 在while循环中,我们使用zis.getNextEntry()方法逐个获取压缩文件中的条目(文件或目录)。如果获取到的条目不是一个目录,则我们将文件计数器" file Count"增加1。 最后,在try块内部,我们输出了文件数量的结果。 如果在处理过程中发生了IOException异常,我们将在catch块中打印异常信息。 请注意,这段代码只计算压缩文件中的文件数量,并不考虑嵌套的目录结构。如果需要计算包括目录在内的所有条目数量,请相应地修改代码。 希望对您有帮助!如果您有任何其他问题,请随时提问。