压缩文件 - 路径上的共享冲突

时间:2015-08-06 07:34:07

标签: c# android

我目前在Android应用程序上工作,我必须开发一个将文件压缩到目录中的函数。

要获取所有文件,我使用DirectoryInfo()和FileInfo()类,然后使用ZipArchive()和ZipArchiveEntry()类来创建我的.zip文件。

我遇到的问题是当我打开文件以使用FileStream进行压缩以将数据复制到ZipArchive条目中时。当我使用OpenRead()函数时,我遇到了路径错误的共享冲突。

我已经检查了我的Manifest中的权限,并尝试了几种方法,比如ZipFile.CreateFromDirectory,但我仍然遇到同样的问题。

提前感谢您的帮助。

这是我的代码:

public void Compress(DirectoryInfo directory)
    {
        string identifiantSC = currentSceneSelected.Id_Scene;
        string typeSceneSelected = currentSceneSelected.type_Scene;

        if (!Directory.Exists (Path.Combine (pathProject, "Archive"))) {
            Directory.CreateDirectory (Path.Combine (pathProject, "Archive"));
        }

        string pathSceneDir = Path.Combine (pathProject, typeSceneSelected + "_" + identifiantSC);
        string pathZipDir = Path.Combine (pathProject, "Archive");

        foreach (var fileToCompress in directory.GetFiles ()) {

            if (!File.Exists (Path.Combine (pathZipDir, typeSceneSelected + "_" + identifiantSC + ".zip"))) {
                try {
                    var fileZip = new FileStream (Path.Combine (pathZipDir, typeSceneSelected + "_" + identifiantSC + ".zip"), FileMode.Create);

                    archive = new ZipArchive (fileZip, ZipArchiveMode.Create);

                    ZipArchiveEntry readmeEntry = archive.CreateEntry (fileToCompress.Name, CompressionLevel.Optimal);
                    FileStream originalFileStream = fileToCompress.OpenRead();
                    originalFileStream.CopyTo (readmeEntry.Open ());

                } catch (FileLoadException e){
                    Console.WriteLine ("FILE LOAD EXCEPTION : " + e);
                } catch (AccessViolationException e){
                    Console.WriteLine ("ACCESS VIOLATION EXCEPTION : " + e);
                } catch (UnauthorizedAccessException e){
                    Console.WriteLine ("ACCESS EXCEPTION : " + e);
                }
            } 
            else {
                using (var fileZip = new FileStream (Path.Combine (pathZipDir, typeSceneSelected + "_" + identifiantSC + ".zip"), FileMode.Open)) {

                    using (archive = new ZipArchive (fileZip, ZipArchiveMode.Update)) {

                        ZipArchiveEntry readmeEntry = archive.CreateEntry (fileToCompress.Name, CompressionLevel.Optimal);

                        FileStream originalFileStream = fileToCompress.OpenRead ();
                        originalFileStream.CopyTo (readmeEntry.Open ());
                        originalFileStream.Close ();
                    }
                }                   
            }
        }
    }

0 个答案:

没有答案