/** 遍历文件夹获取所有文件信息 **/ static void TraverseFolder(string _folderPath) { Debug.Log("遍历文件夹 " + _folderPath); string tempSourceDirpath = _folderPath.Substring(0, _folderPath.LastIndexOf('/')); /** 读取文件夹下面所有文件的信息 **/ DirectoryInfo tempDirInfo = new DirectoryInfo(_folderPath); foreach (FileInfo tempFileInfo in tempDirInfo.GetFiles("*.*", SearchOption.AllDirectories)) { if (tempFileInfo.Extension == ".meta" || tempFileInfo.Extension == ".manifest") { continue; } string tempFileName = tempFileInfo.FullName.Replace("\\", "/"); tempFileName = tempFileName.Replace(tempSourceDirpath + "/", ""); int tempFileSize = (int)tempFileInfo.Length; Debug.Log(id + " : " + tempFileName + " 文件大小: " + tempFileSize); OneFileInfo tempInfo = new OneFileInfo(); tempInfo.id = id; tempInfo.size = tempFileSize; tempInfo.filePath = tempFileName; tempInfo.pathLength = new UTF8Encoding().GetBytes(tempFileName).Length; /** 读取这个文件 **/ FileStream tempFileStreamRead = new FileStream(tempFileInfo.FullName, FileMode.Open, FileAccess.Read); if (tempFileStreamRead == null) { Debug.Log("读取文件失败 : " + tempFileInfo.FullName); return; } else { byte[] tempFileData = new byte[tempFileSize]; tempFileStreamRead.Read(tempFileData, 0, tempFileSize); tempInfo.fileData = tempFileData; } tempFileStreamRead.Close(); allFileInfoDic.Add(id, tempInfo); id++; totalSize += tempFileSize; } }
/** 打包一个文件夹 **/ public static void PackFolder(string _folderPath, string _upkFilePath) { TraverseFolder(_folderPath); Debug.Log("文件数量 : " + id); Debug.Log("文件总大小 : " + totalSize); /** 更新文件在UPK中的起始点 **/ int tempFirstFileStartPos = 0 + 4; for (int tempIndex = 0; tempIndex < allFileInfoDic.Count; tempIndex++) { tempFirstFileStartPos += 4 + 4 + 4 + 4 + allFileInfoDic[tempIndex].pathLength; } int tempStartPos = 0; for (int tempIndex = 0; tempIndex < allFileInfoDic.Count; tempIndex++) { if (tempIndex == 0) { tempStartPos = tempFirstFileStartPos; } else { tempStartPos = allFileInfoDic[tempIndex - 1].startPos + allFileInfoDic[tempIndex - 1].size;//上一个文件的开始+文件大小; } allFileInfoDic[tempIndex].startPos = tempStartPos; } /** 写文件 **/ FileStream tempFileStream = new FileStream(_upkFilePath, FileMode.Create); /** 文件总数量 **/ byte[] tempTotalIdData = System.BitConverter.GetBytes(id); tempFileStream.Write(tempTotalIdData, 0, tempTotalIdData.Length); for (int tempIndex = 0; tempIndex < allFileInfoDic.Count; tempIndex++) { /** 写入ID **/ byte[] tempIdData = System.BitConverter.GetBytes(allFileInfoDic[tempIndex].id); tempFileStream.Write(tempIdData, 0, tempIdData.Length); /** 写入StartPos **/ byte[] tempStartPosData = System.BitConverter.GetBytes(allFileInfoDic[tempIndex].startPos); tempFileStream.Write(tempStartPosData, 0, tempStartPosData.Length); /** 写入size **/ byte[] tempSizeData = System.BitConverter.GetBytes(allFileInfoDic[tempIndex].size); tempFileStream.Write(tempSizeData, 0, tempSizeData.Length); /** 写入pathLength **/ byte[] tempPathLengthData = System.BitConverter.GetBytes(allFileInfoDic[tempIndex].pathLength); tempFileStream.Write(tempPathLengthData, 0, tempPathLengthData.Length); /** 写入path **/ byte[] tempMyPathData = new UTF8Encoding().GetBytes(allFileInfoDic[tempIndex].filePath); tempFileStream.Write(tempMyPathData, 0, tempMyPathData.Length); } /** 写入文件数据 **/ int tempTotalProcessSize = 0; foreach (var tempInfopair in allFileInfoDic) { OneFileInfo info = tempInfopair.Value; int tempSize = info.size; byte[] tempData = null; int tempProcessSize = 0; while (tempProcessSize < tempSize) { if (tempSize - tempProcessSize < 1024) { tempData = new byte[tempSize - tempProcessSize]; } else { tempData = new byte[1024]; } tempFileStream.Write(info.fileData, tempProcessSize, tempData.Length); tempProcessSize += tempData.Length; tempTotalProcessSize += tempData.Length; } } tempFileStream.Flush(); tempFileStream.Close(); /** 重置数据 **/ id = 0; totalSize = 0; allFileInfoDic.Clear(); }
public static IEnumerator UnpackFolder(string _upkFilePath, string _outFilePath, bool _isDownload = false) { int tempOffset = 0; int tempTotalSize = 0; int tempFileCount = 0; allFileInfoDic.Clear(); FileStream tempUpkFilestream = new FileStream(_upkFilePath, FileMode.Open); long tempIdss = tempUpkFilestream.Length; tempUpkFilestream.Seek(0, SeekOrigin.Begin); //读取文件数量; byte[] tempTotaliddata = new byte[4]; tempUpkFilestream.Read(tempTotaliddata, 0, 4); int tempFilecount = BitConverter.ToInt32(tempTotaliddata, 0); tempOffset += 4; //读取所有文件信息; for (int tempIndex = 0; tempIndex < tempFilecount; tempIndex++) { byte[] tempIdData = new byte[4]; tempUpkFilestream.Seek(tempOffset, SeekOrigin.Begin); tempUpkFilestream.Read(tempIdData, 0, 4); int tempId = BitConverter.ToInt32(tempIdData, 0); tempOffset += 4; //读取StartPos; byte[] tempStartPosData = new byte[4]; tempUpkFilestream.Seek(tempOffset, SeekOrigin.Begin); tempUpkFilestream.Read(tempStartPosData, 0, 4); int tempStartPos = BitConverter.ToInt32(tempStartPosData, 0); tempOffset += 4; //读取size; byte[] tempSizeData = new byte[4]; tempUpkFilestream.Seek(tempOffset, SeekOrigin.Begin); tempUpkFilestream.Read(tempSizeData, 0, 4); int tempSize = BitConverter.ToInt32(tempSizeData, 0); tempOffset += 4; //读取pathLength; byte[] tempPathLengthData = new byte[4]; tempUpkFilestream.Seek(tempOffset, SeekOrigin.Begin); tempUpkFilestream.Read(tempPathLengthData, 0, 4); int tempPathLength = BitConverter.ToInt32(tempPathLengthData, 0); tempOffset += 4; //读取path; byte[] tempPathData = new byte[tempPathLength]; tempUpkFilestream.Seek(tempOffset, SeekOrigin.Begin); tempUpkFilestream.Read(tempPathData, 0, tempPathLength); string tempPath = encoding.GetString(tempPathData); tempOffset += tempPathLength; //添加到Dic; OneFileInfo tempInfo = new OneFileInfo(); tempInfo.id = tempId; tempInfo.size = tempSize; tempInfo.filePath = tempPath; tempInfo.startPos = tempStartPos; tempInfo.pathLength = tempPathLength; allFileInfoDic.Add(tempId, tempInfo); tempTotalSize += tempSize; } Events.msInst.DispatchEvent(VersionEvent.UNPACC_SIZE, (float)tempTotalSize); //释放文件; int tempTotalProcessSize = 0; foreach (var tempInfoPair in allFileInfoDic) { OneFileInfo tempInfo = tempInfoPair.Value; int tempSize = tempInfo.size; string tempPath = tempInfo.filePath; int tempStartPos = tempInfo.startPos; //创建文件; string tempDirPath = ""; if (tempPath.IndexOf('/') > -1) { tempDirPath = _outFilePath + tempPath.Substring(0, tempPath.LastIndexOf('/')); } else { tempDirPath = _outFilePath; } string tempFilePath = _outFilePath + tempPath; if (Directory.Exists(tempDirPath) == false) { Directory.CreateDirectory(tempDirPath); } if (File.Exists(tempFilePath)) { File.Delete(tempFilePath); } byte[] tmepFileData; int tempProcessSize = 0; FileStream tempFileStream = new FileStream(tempFilePath, FileMode.Create); while (tempProcessSize < tempSize) { if (tempSize - tempProcessSize < 1024) { tmepFileData = new byte[tempSize - tempProcessSize]; } else { tmepFileData = new byte[1024]; } //读取; tempUpkFilestream.Seek(tempStartPos + tempProcessSize, SeekOrigin.Begin); tempUpkFilestream.Read(tmepFileData, 0, tmepFileData.Length); tempFileStream.Write(tmepFileData, 0, tmepFileData.Length); tempProcessSize += tmepFileData.Length; tempTotalProcessSize += tmepFileData.Length; } if (_isDownload) { yield return(null); } else { if (tempFileCount >= 10) { tempFileCount = 0; yield return(null); } } tempFileCount++; Events.msInst.DispatchEvent(VersionEvent.UNPACC_PROGRESS, ((float)tempTotalProcessSize / (float)tempTotalSize)); tempFileStream.Flush(); tempFileStream.Close(); } tempUpkFilestream.Close(); // 解压完毕,删除文件 File.Delete(_upkFilePath); Events.msInst.DispatchEvent(VersionEvent.UNPACC_COMPLETION, null); }
static bool ExtraUPK(string upkfilepath, string outputpath, ref List <string> pathList, IProgress progress, bool saveToFile, Action <string, byte[]> callback) { m_allFileInfoDic.Clear(); if (!outputpath.EndsWith("/")) { outputpath = outputpath + "/"; } int totalsize = 0; bool success = true; pathList = new List <string>(); FileStream upkFilestream = new FileStream(upkfilepath, FileMode.Open); try { upkFilestream.Seek(0, SeekOrigin.Begin); int offset = 0; //读取文件数量; byte[] totaliddata = new byte[4]; upkFilestream.Read(totaliddata, 0, 4); int filecount = BitConverter.ToInt32(totaliddata, 0); offset += 4; Debug.Log("filecount=" + filecount); //读取所有文件信息; for (int index = 0; index < filecount; index++) { //读取id; byte[] iddata = new byte[4]; upkFilestream.Seek(offset, SeekOrigin.Begin); upkFilestream.Read(iddata, 0, 4); int id = BitConverter.ToInt32(iddata, 0); offset += 4; //读取StartPos; byte[] startposdata = new byte[4]; upkFilestream.Seek(offset, SeekOrigin.Begin); upkFilestream.Read(startposdata, 0, 4); int startpos = BitConverter.ToInt32(startposdata, 0); offset += 4; //读取size; byte[] sizedata = new byte[4]; upkFilestream.Seek(offset, SeekOrigin.Begin); upkFilestream.Read(sizedata, 0, 4); int size = BitConverter.ToInt32(sizedata, 0); offset += 4; //读取pathLength; byte[] pathLengthdata = new byte[4]; upkFilestream.Seek(offset, SeekOrigin.Begin); upkFilestream.Read(pathLengthdata, 0, 4); int pathLength = BitConverter.ToInt32(pathLengthdata, 0); offset += 4; //读取path; byte[] pathdata = new byte[pathLength]; upkFilestream.Seek(offset, SeekOrigin.Begin); upkFilestream.Read(pathdata, 0, pathLength); string path = m_UTF8Encoding.GetString(pathdata); offset += pathLength; //添加到Dic; OneFileInfo info = new OneFileInfo(); info.m_id = id; info.m_Size = size; info.m_PathLength = pathLength; info.m_Path = path; info.m_StartPos = startpos; m_allFileInfoDic.Add(id, info); pathList.Add(path); totalsize += size; // Debug.Log ("id=" + id + " startPos=" + startpos + " size=" + size + " pathLength=" + pathLength + " path=" + path); } //释放文件; int totalprocesssize = 0; foreach (var infopair in m_allFileInfoDic) { OneFileInfo info = infopair.Value; int startPos = info.m_StartPos; int size = info.m_Size; string path = info.m_Path; //创建文件 string dirpath = outputpath + (path.LastIndexOf('/') < 0 ? "" : path.Substring(0, path.LastIndexOf('/'))); string filepath = outputpath + path; if (Directory.Exists(dirpath) == false) { Directory.CreateDirectory(dirpath); } if (File.Exists(filepath)) { File.Delete(filepath); } FileStream fileStream = null; if (saveToFile) { fileStream = new FileStream(filepath, FileMode.Create); } MemoryStream memoryStream = new MemoryStream(); try { byte[] tmpfiledata; int processSize = 0; while (processSize < size) { if (size - processSize < 1024) { tmpfiledata = new byte[size - processSize]; } else { tmpfiledata = new byte[1024]; } //读取; upkFilestream.Seek(startPos + processSize, SeekOrigin.Begin); upkFilestream.Read(tmpfiledata, 0, tmpfiledata.Length); //写入; if (saveToFile) { fileStream.Write(tmpfiledata, 0, tmpfiledata.Length); } memoryStream.Write(tmpfiledata, 0, tmpfiledata.Length); processSize += tmpfiledata.Length; totalprocesssize += tmpfiledata.Length; if (progress != null) { progress.SetProgress((long)totalprocesssize, (long)totalsize); } } if (saveToFile) { fileStream.Flush(); } if (callback != null) { callback(filepath, memoryStream.ToArray()); } } catch (Exception e) { Debug.LogError(e.Message); Debug.LogError(e.StackTrace); success = false; } finally { if (saveToFile) { fileStream.Close(); } memoryStream.Close(); } } } catch (Exception e) { Debug.LogError(e.Message); Debug.LogError(e.StackTrace); success = false; } finally { upkFilestream.Close(); } return(success); }
/** 打包一个文件夹 **/ public static void PackFolder(string folderpath, string upkfilepath, bool ignoreMeta, Ignored ignored = null, IProgress progress = null) { TraverseFolder(folderpath, ignoreMeta, ignored); /** 更新文件在UPK中的起始点 **/ int firstfilestartpos = 0 + 4; for (int index = 0; index < m_allFileInfoDic.Count; index++) { firstfilestartpos += 4 + 4 + 4 + 4 + m_allFileInfoDic[index].m_PathLength; } int startpos = 0; for (int index = 0; index < m_allFileInfoDic.Count; index++) { if (index == 0) { startpos = firstfilestartpos; } else { startpos = m_allFileInfoDic[index - 1].m_StartPos + m_allFileInfoDic[index - 1].m_Size; //上一个文件的开始+文件大小; } m_allFileInfoDic[index].m_StartPos = startpos; } /** 写文件 **/ FileStream fileStream = new FileStream(upkfilepath, FileMode.Create); try { /** 文件总数量 **/ byte[] totaliddata = System.BitConverter.GetBytes(m_id); fileStream.Write(totaliddata, 0, totaliddata.Length); for (int index = 0; index < m_allFileInfoDic.Count; index++) { /** 写入ID **/ byte[] iddata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_id); fileStream.Write(iddata, 0, iddata.Length); /** 写入StartPos **/ byte[] startposdata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_StartPos); fileStream.Write(startposdata, 0, startposdata.Length); /** 写入size **/ byte[] sizedata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_Size); fileStream.Write(sizedata, 0, sizedata.Length); /** 写入pathLength **/ byte[] pathLengthdata = System.BitConverter.GetBytes(m_allFileInfoDic[index].m_PathLength); fileStream.Write(pathLengthdata, 0, pathLengthdata.Length); /** 写入path **/ byte[] mypathdata = new UTF8Encoding().GetBytes(m_allFileInfoDic[index].m_Path); fileStream.Write(mypathdata, 0, mypathdata.Length); } int totalprocessSize = 0; foreach (var infopair in m_allFileInfoDic) { OneFileInfo info = infopair.Value; int size = info.m_Size; byte[] tmpdata = null; int processSize = 0; while (processSize < size) { if (size - processSize < 1024) { tmpdata = new byte[size - processSize]; } else { tmpdata = new byte[1024]; } fileStream.Write(info.m_data, processSize, tmpdata.Length); processSize += tmpdata.Length; totalprocessSize += tmpdata.Length; if (progress != null) { progress.SetProgress(m_totalSize, totalprocessSize); } } } fileStream.Flush(); } finally { fileStream.Close(); } /** 重置数据 **/ m_id = 0; m_totalSize = 0; m_allFileInfoDic.Clear(); }
/** 遍历文件夹获取所有文件信息 **/ public static void TraverseFolder(string folderpath, bool ignoreMeta, Ignored customIgnore = null) { DirectoryInfo tmpDirectoryInfo = new DirectoryInfo(folderpath); folderpath = tmpDirectoryInfo.FullName.Replace("\\", "/"); if (!folderpath.EndsWith("/")) { folderpath = folderpath + "/"; } Debug.Log("遍历文件夹: " + folderpath); /** 读取文件夹下面所有文件的信息 **/ DirectoryInfo dirInfo = new DirectoryInfo(folderpath); m_id = 0; m_totalSize = 0; m_allFileInfoDic.Clear(); foreach (FileInfo fileinfo in dirInfo.GetFiles("*.*", SearchOption.AllDirectories)) { if (fileinfo.Name.Contains(".DS_Store")) { continue; } if (ignoreMeta && fileinfo.Extension == ".meta") { continue; } if (customIgnore != null && customIgnore(fileinfo)) { continue; } string filename = fileinfo.FullName.Replace("\\", "/"); filename = filename.Replace(folderpath, ""); int filesize = (int)fileinfo.Length; // Debug.Log (m_id + " : " + filename + " 文件大小: " + filesize); OneFileInfo info = new OneFileInfo(); info.m_id = m_id; info.m_Size = filesize; info.m_Path = filename; info.m_PathLength = new UTF8Encoding().GetBytes(filename).Length; /** 读取这个文件 **/ FileStream fileStreamRead = new FileStream(fileinfo.FullName, FileMode.Open, FileAccess.Read); if (fileStreamRead == null) { Debug.Log("读取文件失败: " + fileinfo.FullName); return; } else { byte[] filedata = new byte[filesize]; fileStreamRead.Read(filedata, 0, filesize); info.m_data = filedata; } fileStreamRead.Close(); m_allFileInfoDic.Add(m_id, info); m_id++; m_totalSize += filesize; } Debug.Log("文件数量 : " + m_id); Debug.Log("文件总大小 : " + m_totalSize); }