项目中遇到需求:需要预置文件夹到sdcard路径下,sdcard路径是开机启动时才进行挂载,所以没有办法在ROM制作的时候进行拷贝。
解决方案:
1、将需要存放到sdcard路径下的文件夹打包成zip文件
2、放到apk的assets路径下
3、apk加载后,把assets路径的zip文件,拷贝到sdcard路径,然后再进行解压。
应用共有三个类:
1.MainActivity.java
2.CopyIntentService.java
3.ZipUtils.java
下面就介绍这几个类,MainActivity职责就是启动CopyIntentService,代码如下:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this, CopyIntentService.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setPackage(getPackageName());
startService(i);
}
}
然后是CopyIntentService类,这个类主要是处理拷贝文件和解压文件,代码如下:
public class CopyIntentService extends IntentService {
private static String SD_INTERNAL_PATH = "/sdcard/jinghua/";
private static String BACKUP_PATH = SD_INTERNAL_PATH;
private static String FILE_NAME = "word.zip";
public static String FLDY_PREFER = "file_copy";
public static String IS_COPY_FULL = "is_copy_full";
public static String IS_UNZIP_FULL = "is_unzip_full";
private File file;
private SharedPreferences preferences;
public CopyIntentService() {
super("CopyIntentService");
}
@Override
public IBinder onBind(Intent intent) {
return super.onBind(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
preferences = getSharedPreferences(CopyIntentService.FLDY_PREFER,
Context.MODE_PRIVATE);
if (!preferences.getBoolean(IS_COPY_FULL, false)) {
try {
file = new File(SD_PATH + FILE_NAME);
if (file.exists())
file.delete();
file.createNewFile();
copyAssetsZipFile();
} catch (Exception e) {
preferences.edit().putBoolean(IS_COPY_FULL, false).commit();
return;
}
}
if (!preferences.getBoolean(IS_UNZIP_FULL, false)) {
try {
ZipUtils.unZipFile(file, BACKUP_PATH);
preferences.edit().putBoolean(IS_UNZIP_FULL, true).commit();
file.delete();
} catch (Exception e) {
preferences.edit().putBoolean(IS_UNZIP_FULL, false).commit();
return;
}
}
}
private void copyAssetsZipFile() throws IOException {
int length = 0;
int totalSize = 0;
byte[] buffer = new byte[2048];
InputStream myInput = getAssets().open(FILE_NAME);
OutputStream myOutput = new FileOutputStream(file);
while ((length = myInput.read(buffer)) > 0) {
totalSize += length;
myOutput.write(buffer, 0, length);
}
if (totalSize == myInput.available()
&& myInput.available() == file.length())
preferences.edit().putBoolean(IS_COPY_FULL, true).commit();
else
preferences.edit().putBoolean(IS_COPY_FULL, false).commit();
myOutput.flush();
myOutput.close();
myInput.close();
}
}
最后是ZipUtils类,这是一个工具类,主要负责将解压已拷贝到sdcard路径下的文件,代码如下:
public class ZipUtils {
private static final int BUFF_SIZE = 1024 * 1024; // 1M Byte
public static void zipFiles(Collection<File> resFileList, File zipFile)
throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFile), BUFF_SIZE));
zipout.setMethod(ZipOutputStream.STORED);
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.close();
}
public static void zipFiles(Collection<File> resFileList, File zipFile,
String comment) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.setComment(comment);
zipout.close();
}
public static void unZipFile(File zipFile, String folderPath)
throws ZipException, IOException {
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
File desFile = new File(str);
if (entry.isDirectory()) {
if (!desFile.exists())
desFile.mkdirs();
continue;
}
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}
public static ArrayList<File> unZipSelectedFile(File zipFile,
String folderPath, String nameContains) throws ZipException,
IOException {
ArrayList<File> fileList = new ArrayList<File>();
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdir();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
if (entry.getName().contains(nameContains)) {
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
fileList.add(desFile);
}
}
return fileList;
}
public static ArrayList<String> getEntriesNames(File zipFile)
throws ZipException, IOException {
ArrayList<String> entryNames = new ArrayList<String>();
Enumeration<?> entries = getEntriesEnumeration(zipFile);
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
entryNames.add(new String(getEntryName(entry)));
}
return entryNames;
}
public static Enumeration<?> getEntriesEnumeration(File zipFile)
throws ZipException, IOException {
ZipFile zf = new ZipFile(zipFile);
return zf.entries();
}
public static String getEntryComment(ZipEntry entry)
throws UnsupportedEncodingException {
return new String(entry.getComment());
}
public static String getEntryName(ZipEntry entry)
throws UnsupportedEncodingException {
return new String(entry.getName());
}
private static void zipFile(File resFile, ZipOutputStream zipout,
String rootpath) throws FileNotFoundException, IOException {
rootpath = rootpath
+ (rootpath.trim().length() == 0 ? "" : File.separator)
+ resFile.getName();
rootpath = new String(rootpath);
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(resFile), BUFF_SIZE);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
}
使用方法是将你需要放到sdcard路径下的文件夹打包成zip文件,然后放到assets路径下,就可以了~