Partilhar via


Windows Phone 7.5에서 압축 사용

최초 문서 게시일: 2012년 4월 14일 토요일

참고: 이전 게시물과 마찬가지로 이 게시물에도 Word 버전이 첨부되어 있습니다. 이 사이트의 서식으로 인해 내용이 제대로 표시되지 않으면 Word 버전을 확인하시기 바랍니다.  

Windows Phone 관련 개발을 진행한 분이라면 대부분 알고 계시겠지만, Windows Phone에서는 압축 및 압축 풀기가 기본적으로 제공되지 않습니다. 따라서 많은 개발자들이 CodePlex의 SharpCompress 프로젝트를 사용하고 있으며, 저 역시 SharpCompress를 사용합니다. 그러나 휴대폰에서 파일 압축을 푸는 과정이 포함된 설명서는 제공되지 않고 있는데요. 개인적으로는 WriteAllToDirectory 메서드를 사용하여 Windows에서 압축된 파일의 압축을 푸는 winform 응용 프로그램용으로 다수의 코드를 작성하기도 했습니다. 그러나 Windows Phone 어셈블리에는 이 메서드가 없습니다. 또한 SharpCompress용 Windows Phone 어셈블리 사용 시 압축을 풀 수 있는 경로만 제공할 수는 없으며 스트림 개체를 지정해야 한다는 문제도 있습니다.

이 방법도 작동하기는 하지만, Windows 정식 버전에서 이 방법을 사용할 때와는 크게 다른 방식으로 작업을 수행해야 합니다. 해당 작업을 수행하기 위한 몇 가지 팁은 다음과 같습니다.

1. Reader 팩터리에서 가져온 IReader의 Entry 컬렉션을 열거합니다.

//"sr" is a stream reader object from previous code that contains the byte array of the actual zip file

using (var reader = ReaderFactory.Open(sr))

{

while (reader.MoveToNextEntry())

{

   //IsDirectory always returns false in everything I've seen so far

   if (!reader.Entry.IsDirectory)

   {

      //process the entry, described next

   }

   else

   {

       //do not know of a scenario where we can end up here

       Debug.WriteLine(reader.Entry.FilePath);

   }

}

}

2. 파일용 디렉터리를 만든 다음 쓰기가 가능한 IsolatedStorageFileStream 인스턴스를 가져옵니다.

//IsDirectory always returns false in everything I've seen so far

if (!reader.Entry.IsDirectory)

{

       IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);

}

 

private IsolatedStorageFileStream GetLocalFile(string fullPath)

{

   IsolatedStorageFileStream fs = null;

 

   try

   {

       //get isolated storage so we can create directories and decompress

       IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

 

       //trim everything from the last index of /, which is the file

       //name, i.e. it will look like folder/folder/folder/filename.txt

       string dirPath = fullPath.Substring(0, fullPath.LastIndexOf("/"));

 

       //what's nice is you can create the full folder path in one call

       if (!storage.DirectoryExists(dirPath))

          storage.CreateDirectory(dirPath);

 

       //now that all the directories exist, create a stream for the

       // file – again, cool because you can just give the full path

       fs = storage.CreateFile(fullPath);

   }

   catch (Exception ex)

   {

       Debug.WriteLine(ex.Message);

   }

 

   return fs;

}

3. 스트림을 가져왔으므로 WriteEntryTo 메서드를 사용하여 개별 파일을 쓸 수 있습니다.

if (!reader.Entry.IsDirectory)

{

   IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);

 

   if (theFile != null)

       reader.WriteEntryTo(theFile);

}

 

설명서가 제공되지 않아 이렇게 게시물을 통해서 설명을 드리는 점 죄송하게 생각합니다. 이 게시물이 도움이 되기를 바랍니다.

이 문서는 번역된 블로그 게시물입니다. 원본 문서는 Using Compression on Windows Phone 7.5를 참조하십시오.