package com.mobimtech.natives.ivp.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.mobimtech.natives.ivp.chatroom.RoomCommonData;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
public class FileTool {
private static final String xmlPath="gift.xml";
private static final String zipPath="gift.zip";
/**
* DeCompress the ZIP to the path
* @param zipFileString name of ZIP
* @param outPathString path to be unZIP
* @throws Exception
*/
public static void UnZipFromAssetToPackage(Context context) {
// ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
String outPath=RoomCommonData.outPath;
ZipInputStream inZip;
try {
inZip = new ZipInputStream(context.getAssets().open(zipPath));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPath + File.separator + szName);
folder.mkdirs();
} else {
String test=outPath + File.separator + szName;
File file = new File(outPath + File.separator + szName);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//read InputStream from gift.xml file in path /data/data/packagename
public static InputStream readXmlFromPackage()
{
String outPath=RoomCommonData.outPath;
//get gift data from assets folder.
InputStream inStream=null;
try{
File file=new File(outPath+File.separator+"gift.xml");//从/data/data/cache中读
inStream=new FileInputStream(file);
}catch(IOException e){
e.printStackTrace();
}
return inStream;
}
public static InputStream readXmlFromAsset(Context context){
InputStream inStream=null;
try{
inStream=context.getAssets().open(xmlPath);
}catch(Exception e){
e.printStackTrace();
}
return inStream;
}
//read bitmap from path /data/data/packagename/
public static Bitmap readBitmapFromLocal(String fileName){
Bitmap bitmap = null;
bitmap=BitmapFactory.decodeFile(fileName);
return bitmap;
}
/**
* file exist or not
* @param path: full path,ie:/data/data/com.mobimtech.natives.ivp.chatroom/files/json.txt
* @return boolean
*/
public static boolean isFileExist(String path){
if(path==null)
return false;
try{
File f=new File(path);
boolean isexist=f.exists();
if(!f.exists())
return false;
}catch(Exception e){
e.printStackTrace();
}
return true;
}
//save data to file
public static void saveFile(Context context,String srcData,String desFileName){
//context.getFilesDir(); get system file path /data/data/<package name>/files
//context.getCacheDir(); //get cache file path /data/data/<package name>/cache
try{
FileOutputStream outputStream=context.openFileOutput(desFileName, Context.MODE_PRIVATE);
outputStream.write(srcData.getBytes());
outputStream.flush();
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
//read data from file
public static String readFile(Context context,String fileName){
String result="";
try{
FileInputStream inputStream=context.openFileInput(fileName);
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
outputStream.close();
byte[] data=outputStream.toByteArray();
result=new String(data);
}catch(Exception e){
e.printStackTrace();
}
return result;
}
//save data to SDCard
public static void saveFileToSDCard(Context context,String fileName){
try{
Environment.getExternalStorageDirectory(); //得到sdcard目录
File file=new File("/sdcard",fileName);
FileOutputStream outputStream=new FileOutputStream(file);
outputStream.write(fileName.getBytes());
outputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* Compress file and folder
* @param srcFileString file or folder to be Compress
* @param zipFileString the path name of result ZIP
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {
//create ZIP
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
//create the file
File file = new File(srcFileString);
//compress
ZipFiles(file.getParent()+File.separator, file.getName(), outZip);
//finish and close
outZip.finish();
outZip.close();
}
/**
* compress files
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{
if(zipOutputSteam == null)
return;
File file = new File(folderString+fileString);
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(fileString);
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while((len=inputStream.read(buffer)) != -1)
{
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
}
else {
//folder
String fileList[] = file.list();
//no child file and compress
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(fileString+File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
//child files and recursion
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam);
}//end of for
}
}
/**
* return the InputStream of file in the ZIP
* @param zipFileString name of ZIP
* @param fileString name of file in the ZIP
* @return InputStream
* @throws Exception
*/
public static InputStream UpZip(String zipFileString, String fileString)throws Exception {
ZipFile zipFile = new ZipFile(zipFileString);
ZipEntry zipEntry = zipFile.getEntry(fileString);
return zipFile.getInputStream(zipEntry);
}
/**
* return files list(file and folder) in the ZIP
* @param zipFileString ZIP name
* @param bContainFolder contain folder or not
* @param bContainFile contain file or not
* @return
* @throws Exception
*/
public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception {
List<File> fileList = new ArrayList<File>();
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(szName);
if (bContainFolder) {
fileList.add(folder);
}
} else {
File file = new File(szName);
if (bContainFile) {
fileList.add(file);
}
}
}
inZip.close();
return fileList;
}
}
FileTool类
最新推荐文章于 2020-06-15 14:39:43 发布