Android zip文件压缩解压缩

/**
07  *  文件夹遍历
08  * @author once
09  *
10  */
11 public class DirTraversal {
12      
13     //no recursion
14     public static LinkedList<File> listLinkedFiles(String strPath) {
15         LinkedList<File> list = new LinkedList<File>();
16         File dir = new File(strPath);
17         File file[] = dir.listFiles();
18         for (int i = 0; i < file.length; i++) {
19             if (file[i].isDirectory())
20                 list.add(file[i]);
21             else
22                 System.out.println(file[i].getAbsolutePath());
23         }
24         File tmp;
25         while (!list.isEmpty()) {
26             tmp = (File) list.removeFirst();
27             if (tmp.isDirectory()) {
28                 file = tmp.listFiles();
29                 if (file == null)
30                     continue;
31                 for (int i = 0; i < file.length; i++) {
32                     if (file[i].isDirectory())
33                         list.add(file[i]);
34                     else
35                         System.out.println(file[i].getAbsolutePath());
36                 }
37             else {
38                 System.out.println(tmp.getAbsolutePath());
39             }
40         }
41         return list;
42     }
43  
44      
45     //recursion
46     public static ArrayList<File> listFiles(String strPath) {
47         return refreshFileList(strPath);
48     }
49  
50     public static ArrayList<File> refreshFileList(String strPath) {
51         ArrayList<File> filelist = new ArrayList<File>();
52         File dir = new File(strPath);
53         File[] files = dir.listFiles();
54  
55         if (files == null)
56             return null;
57         for (int i = 0; i < files.length; i++) {
58             if (files[i].isDirectory()) {
59                 refreshFileList(files[i].getAbsolutePath());
60             else {
61                 if(files[i].getName().toLowerCase().endsWith("zip"))
62                     filelist.add(files[i]);
63             }
64         }
65         return filelist;
66     }
67 }





/**
013  * Java utils 实现的Zip工具
014  *
015  * @author once
016  */
017 public class ZipUtils {
018     private static final int BUFF_SIZE = 1024 1024// 1M Byte
019  
020     /**
021      * 批量压缩文件(夹)
022      *
023      * @param resFileList 要压缩的文件(夹)列表
024      * @param zipFile 生成的压缩文件
025      * @throws IOException 当压缩过程出错时抛出
026      */
027     public static void zipFiles(Collection<File> resFileList, File zipFile) throwsIOException {
028         ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(newFileOutputStream(
029                 zipFile), BUFF_SIZE));
030         for (File resFile : resFileList) {
031             zipFile(resFile, zipout, "");
032         }
033         zipout.close();
034     }
035  
036     /**
037      * 批量压缩文件(夹)
038      *
039      * @param resFileList 要压缩的文件(夹)列表
040      * @param zipFile 生成的压缩文件
041      * @param comment 压缩文件的注释
042      * @throws IOException 当压缩过程出错时抛出
043      */
044     public static void zipFiles(Collection<File> resFileList, File zipFile, String comment)
045             throws IOException {
046         ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(newFileOutputStream(
047                 zipFile), BUFF_SIZE));
048         for (File resFile : resFileList) {
049             zipFile(resFile, zipout, "");
050         }
051         zipout.setComment(comment);
052         zipout.close();
053     }
054  
055     /**
056      * 解压缩一个文件
057      *
058      * @param zipFile 压缩文件
059      * @param folderPath 解压缩的目标目录
060      * @throws IOException 当解压缩过程出错时抛出
061      */
062     public static void upZipFile(File zipFile, String folderPath) throws ZipException, IOException {
063         File desDir = new File(folderPath);
064         if (!desDir.exists()) {
065             desDir.mkdirs();
066         }
067         ZipFile zf = new ZipFile(zipFile);
068         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
069             ZipEntry entry = ((ZipEntry)entries.nextElement());
070             InputStream in = zf.getInputStream(entry);
071             String str = folderPath + File.separator + entry.getName();
072             str = new String(str.getBytes("8859_1"), "GB2312");
073             File desFile = new File(str);
074             if (!desFile.exists()) {
075                 File fileParentDir = desFile.getParentFile();
076                 if (!fileParentDir.exists()) {
077                     fileParentDir.mkdirs();
078                 }
079                 desFile.createNewFile();
080             }
081             OutputStream out = new FileOutputStream(desFile);
082             byte buffer[] = new byte[BUFF_SIZE];
083             int realLength;
084             while ((realLength = in.read(buffer)) > 0) {
085                 out.write(buffer, 0, realLength);
086             }
087             in.close();
088             out.close();
089         }
090     }
091  
092     /**
093      * 解压文件名包含传入文字的文件
094      *
095      * @param zipFile 压缩文件
096      * @param folderPath 目标文件夹
097      * @param nameContains 传入的文件匹配名
098      * @throws ZipException 压缩格式有误时抛出
099      * @throws IOException IO错误时抛出
100      */
101     public static ArrayList<File> upZipSelectedFile(File zipFile, String folderPath,
102             String nameContains) throws ZipException, IOException {
103         ArrayList<File> fileList = new ArrayList<File>();
104  
105         File desDir = new File(folderPath);
106         if (!desDir.exists()) {
107             desDir.mkdir();
108         }
109  
110         ZipFile zf = new ZipFile(zipFile);
111         for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
112             ZipEntry entry = ((ZipEntry)entries.nextElement());
113             if (entry.getName().contains(nameContains)) {
114                 InputStream in = zf.getInputStream(entry);
115                 String str = folderPath + File.separator + entry.getName();
116                 str = new String(str.getBytes("8859_1"), "GB2312");
117                 // str.getBytes("GB2312"),"8859_1" 输出
118                 // str.getBytes("8859_1"),"GB2312" 输入
119                 File desFile = new File(str);
120                 if (!desFile.exists()) {
121                     File fileParentDir = desFile.getParentFile();
122                     if (!fileParentDir.exists()) {
123                         fileParentDir.mkdirs();
124                     }
125                     desFile.createNewFile();
126                 }
127                 OutputStream out = new FileOutputStream(desFile);
128                 byte buffer[] = new byte[BUFF_SIZE];
129                 int realLength;
130                 while ((realLength = in.read(buffer)) > 0) {
131                     out.write(buffer, 0, realLength);
132                 }
133                 in.close();
134                 out.close();
135                 fileList.add(desFile);
136             }
137         }
138         return fileList;
139     }
140  
141     /**
142      * 获得压缩文件内文件列表
143      *
144      * @param zipFile 压缩文件
145      * @return 压缩文件内文件名称
146      * @throws ZipException 压缩文件格式有误时抛出
147      * @throws IOException 当解压缩过程出错时抛出
148      */
149     public static ArrayList<String> getEntriesNames(File zipFile) throws ZipException, IOException {
150         ArrayList<String> entryNames = new ArrayList<String>();
151         Enumeration<?> entries = getEntriesEnumeration(zipFile);
152         while (entries.hasMoreElements()) {
153             ZipEntry entry = ((ZipEntry)entries.nextElement());
154             entryNames.add(new String(getEntryName(entry).getBytes("GB2312"), "8859_1"));
155         }
156         return entryNames;
157     }
158  
159     /**
160      * 获得压缩文件内压缩文件对象以取得其属性
161      *
162      * @param zipFile 压缩文件
163      * @return 返回一个压缩文件列表
164      * @throws ZipException 压缩文件格式有误时抛出
165      * @throws IOException IO操作有误时抛出
166      */
167     public static Enumeration<?> getEntriesEnumeration(File zipFile) throws ZipException,
168             IOException {
169         ZipFile zf = new ZipFile(zipFile);
170         return zf.entries();
171  
172     }
173  
174     /**
175      * 取得压缩文件对象的注释
176      *
177      * @param entry 压缩文件对象
178      * @return 压缩文件对象的注释
179      * @throws UnsupportedEncodingException
180      */
181     public static String getEntryComment(ZipEntry entry) throwsUnsupportedEncodingException {
182         return new String(entry.getComment().getBytes("GB2312"), "8859_1");
183     }
184  
185     /**
186      * 取得压缩文件对象的名称
187      *
188      * @param entry 压缩文件对象
189      * @return 压缩文件对象的名称
190      * @throws UnsupportedEncodingException
191      */
192     public static String getEntryName(ZipEntry entry) throws UnsupportedEncodingException {
193         return new String(entry.getName().getBytes("GB2312"), "8859_1");
194     }
195  
196     /**
197      * 压缩文件
198      *
199      * @param resFile 需要压缩的文件(夹)
200      * @param zipout 压缩的目的文件
201      * @param rootpath 压缩的文件路径
202      * @throws FileNotFoundException 找不到文件时抛出
203      * @throws IOException 当压缩过程出错时抛出
204      */
205     private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
206             throws FileNotFoundException, IOException {
207         rootpath = rootpath + (rootpath.trim().length() == 0 "" : File.separator)
208                 + resFile.getName();
209         rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
210         if (resFile.isDirectory()) {
211             File[] fileList = resFile.listFiles();
212             for (File file : fileList) {
213                 zipFile(file, zipout, rootpath);
214             }
215         else {
216             byte buffer[] = new byte[BUFF_SIZE];
217             BufferedInputStream in = new BufferedInputStream(newFileInputStream(resFile),
218                     BUFF_SIZE);
219             zipout.putNextEntry(new ZipEntry(rootpath));
220             int realLength;
221             while ((realLength = in.read(buffer)) != -1) {
222                 zipout.write(buffer, 0, realLength);
223             }
224             in.close();
225             zipout.flush();
226             zipout.closeEntry();
227         }
228     }
229 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android解压缩文件时出现"not a directory"(不是目录)错误时,这通常表示解压缩的对象不是一个有效的目录。 出现这个错误的可能原因之一是指定的路径或文件名错误。确保你提供的路径和文件名是正确的,并且指向一个真正存在的目录。 另一个原因是解压缩的对象可能不是一个目录,而是一个文件。在Android中,解压缩函数通常期望目标路径是一个目录,而不是一个文件。如果你要解压缩的是一个文件,而不是目录,你需要修改解压缩的逻辑以适应这种情况。 解决这个问题的一种方法是在解压缩之前检查目标路径是否是一个目录。可以使用File类的isDirectory()方法来判断目标路径是否是一个目录。如果不是一个目录,你可以手动创建一个目录,然后使用解压缩函数将文件解压到这个新创建的目录中。 以下是一个示例代码片段,使用Apache Commons Compress库来解压缩文件: ``` File sourceFile = new File("path/to/source/file.zip"); File destDir = new File("path/to/destination/dir"); if (!destDir.isDirectory()) { destDir.mkdirs(); // 如果目标路径不是一个目录,创建一个新目录 } try (InputStream inputStream = new FileInputStream(sourceFile); ArchiveInputStream ais = new ArchiveStreamFactory() .createArchiveInputStream("zip", inputStream)) { ArchiveEntry entry; while ((entry = ais.getNextEntry()) != null) { File entryFile = new File(destDir, entry.getName()); if (entry.isDirectory()) { entryFile.mkdirs(); // 如果是目录,创建一个新目录 } else { try (OutputStream outputStream = new FileOutputStream(entryFile)) { IOUtils.copy(ais, outputStream); // 拷贝文件内容 } } } } catch (IOException e) { e.printStackTrace(); } ``` 希望这些信息对你有帮助,如果问题仍然存在,请提供更多的细节和相关代码以便更好地帮助你解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值