經(jīng)常對(duì)一些文件,文件夾操作,自己寫一個(gè)不是難事.但有個(gè)可以撿的應(yīng)該也不是壞事.
將destFileName解壓到mExtractToDir??目錄:
public
?
void
?extract(destFileName,mExtractToDir?)?

?
{?
??String?fileName?
=
?
new
?String();?
??String?destFileName?
=
?
new
?String();?
??
??
//
do?our?own?buffering;?reuse?the?same?
??buffer?
byte
[]?buffer?
=
?
new
?
byte
[
16384
];?
??
try
?

??
{?
???ZipFile?archive?
=
?
new
?ZipFile(?mZipFile?);?
???
for
?(?Enumeration?e?
=
?archive.entries();?e.hasMoreElements();?)?

???
{?
????
//
get?the?next?entry?in?the?archive?
????ZipEntry?entry?
=
?(ZipEntry)e.nextElement();?
????
????
if
?(?
!
?entry.isDirectory()?)?

????
{?
?????fileName?
=
?entry.getName();?
?????fileName?
=
?fileName.replace(
'
/
'
,?File.separatorChar);?
?????
?????destFileName?
=
?mExtractToDir?
+
?fileName;?
?????File?destFile?
=
?
new
?File(destFileName);
?????
?????
//
create?the?destination?path,?if?needed?????
?????String?parent?
=
?destFile.getParent();?
?????
if
?(?parent?
!=
?
null
?)?

?????
{?
??????File?parentFile?
=
?
new
?File(parent);?
??????
if
?(?
!
?parentFile.exists()?)?

??????
{?
???????
//
create?the?chain?of?subdirs?to?the?file?
???????parentFile.mkdirs();?
??????}
?
?????}
?
?????
?????
//
get?a?stream?of?the?archive?entry's?bytes?
?????InputStream?in?
=
?archive.getInputStream(entry);?
?????
?????
//
open?a?stream?to?the?destination?
?????file?OutputStream?out?
=
?
new
?FileOutputStream(destFileName);?
?????
?????
//
Repeat?reading?into?buffer?and?writing?buffer?to?file,?
?????
//
until?done.?Count?will?always?be?#?bytes?read,?until?
//
EOF?when?it?is?-1.?
?????
int
?count;?
?????
while
?(?(count?
=
?in.read(buffer))?
!=
?
-
1
?)?

?????
{?
??????out.write(buffer,?
0
,?count?);?
?????}
?in.close();?out.close();?
?????}
?
????}
?
???}
?
??
catch
(?ZipException?ze?)?

??
{?
???ze.printStackTrace();?
??}
?
??
catch
?(?NullPointerException?npe?)?

??
{?
???npe.printStackTrace();?
??}
?
??
catch
?(?IOException?ioe?)?

??
{?
???ioe.printStackTrace();?
??}
?
??
catch
?(?SecurityException?se?)

??
{?
???se.printStackTrace();?
??}
?
?}
?
將某一文件夾的內(nèi)容清空:
public
?
void
?cleanImportDirectory(String?iPath)?

?
{?
??
try
?

??
{?
???File?theFile?
=
?
new
?File(iPath);?
???File?allFiles[]?
=
?theFile.listFiles();?
???
???
for
?(
int
?i?
=
?
0
;?i?
<
?allFiles.length;?i
++
)?

???
{?
????
if
(allFiles[i].isDirectory())?

????
{?
?????cleanImportDirectory(allFiles[i].toString());?
?????allFiles[i].delete();?
????}
?
????
else
????
{?
?????allFiles[i].delete();?
????}
?
???}
?
??}
?
??
catch
?(NullPointerException?npe)?

??
{?
???mLogger.severe(iPath?
+
?
"
?did?not?exist?and?was?not?cleaned!!
"
);?
??}
?
?}
?
?
將某一文件夾的內(nèi)容移到指定文件夾:
private
?
void
?copyCourse(String?iInFilePath,?String?iOutFilePath)?
throws
?IOException?

?
{?
??
try
?

??
{?
???String?inDirName?
=
?iInFilePath;?
???inDirName.replace(
'
/
'
,?java.io.File.separatorChar);?
???
???File?tempFile?
=
?
new
?File(inDirName);?
???File[]?fileNames?
=
?tempFile.listFiles();?
???
???String?outDirName?
=
?iOutFilePath;?
???outDirName?
=
?outDirName.replace(
'
/
'
,?java.io.File.separatorChar);?
???File?tempDir?
=
?
new
?File(outDirName);?
???tempDir.mkdirs();?
???
for
?(
int
?i?
=
?
0
;?i?
<
?fileNames.length;?i
++
)?

???
{?
????String?tempString?
=
?outDirName?
+
?java.io.File.separatorChar?
+
?fileNames[i].getName();?
????
if
(fileNames[i].isDirectory())?

????
{?
?????File?dirToCreate?
=
?
new
?File(tempString);?
?????dirToCreate.mkdirs();?
?????copyCourse(fileNames[i].getAbsolutePath(),?tempString);?
????}
????
else
?

????
{?
?????BufferedInputStream?in?
=
?
new
?BufferedInputStream(
new
?FileInputStream(fileNames[i]));?
?????BufferedOutputStream?out?
=
?
new
?BufferedOutputStream(
new
?FileOutputStream(tempString));?
?????
int
?c;?
?????
while
((c?
=
?in.read())?
!=
?
-
1
)?

?????
{?
??????out.write(c);?
?????}
?????
?????in.close();?
?????out.close();?
????}
?
???}
?
??}
?
??
catch
?(IOException?IOE)?

??
{?
????IOE.printStackTrace();?
??}
?
我暫時(shí)只碰到了這三個(gè)...