2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "ZipDirectory.h"
22 #include "utils/log.h"
23 #include "utils/CharsetConverter.h"
24 #include "utils/URIUtils.h"
27 #include "ZipManager.h"
35 CZipDirectory::CZipDirectory()
39 CZipDirectory::~CZipDirectory()
43 bool CZipDirectory::GetDirectory(const CStdString& strPathOrig, CFileItemList& items)
47 /* if this isn't a proper archive path, assume it's the path to a archive file */
48 if( !strPathOrig.Left(6).Equals("zip://") )
49 URIUtils::CreateArchivePath(strPath, "zip", strPathOrig, "");
51 strPath = strPathOrig;
55 CStdString strArchive = url.GetHostName();
56 CStdString strOptions = url.GetOptions();
57 CStdString strPathInZip = url.GetFileName();
59 url.SetOptions(""); // delete options to have a clean path to add stuff too
60 url.SetFileName(""); // delete filename too as our names later will contain it
62 CStdString strSlashPath = url.Get();
66 // the RAR code depends on things having a "/" at the end of the path
67 URIUtils::AddSlashAtEnd(strSlashPath);
69 vector<SZipEntry> entries;
70 // turn on fast lookups
71 bool bWasFast(items.GetFastLookup());
72 items.SetFastLookup(true);
73 if (!g_ZipManager.GetZipList(strPath,entries))
76 vector<CStdString> baseTokens;
77 if (!strPathInZip.IsEmpty())
78 CUtil::Tokenize(strPathInZip,baseTokens,"/");
80 for (vector<SZipEntry>::iterator ze=entries.begin();ze!=entries.end();++ze)
82 CStdString strEntryName(ze->name);
83 strEntryName.Replace('\\','/');
84 if (strEntryName == strPathInZip) // skip the listed dir
87 vector<CStdString> pathTokens;
88 CUtil::Tokenize(strEntryName,pathTokens,"/");
89 if (pathTokens.size() < baseTokens.size()+1)
94 for ( unsigned int i=0;i<baseTokens.size();++i )
96 if (pathTokens[i] != baseTokens[i])
101 strEntryName += pathTokens[i] + "/";
106 strEntryName += pathTokens[baseTokens.size()];
107 char c=ze->name[strEntryName.size()];
108 if (c == '/' || c == '\\')
110 bool bIsFolder = false;
111 if (strEntryName[strEntryName.size()-1] != '/') // this is a file
113 strBuffer = strSlashPath + strEntryName + strOptions;
116 { // this is new folder. add if not already added
118 strBuffer = strSlashPath + strEntryName + strOptions;
119 if (items.Contains(strBuffer)) // already added
123 CFileItemPtr pFileItem(new CFileItem);
125 if (g_charsetConverter.isValidUtf8(pathTokens[baseTokens.size()]))
126 g_charsetConverter.utf8ToStringCharset(pathTokens[baseTokens.size()]);
128 pFileItem->SetLabel(pathTokens[baseTokens.size()]);
131 pFileItem->m_dwSize = 0;
132 URIUtils::AddSlashAtEnd(strBuffer);
135 pFileItem->m_dwSize = ze->usize;
136 pFileItem->SetPath(strBuffer);
137 pFileItem->m_bIsFolder = bIsFolder;
138 pFileItem->m_idepth = ze->method;
139 items.Add(pFileItem);
141 items.SetFastLookup(bWasFast);
145 bool CZipDirectory::ContainsFiles(const CStdString& strPath)
147 vector<SZipEntry> items;
148 g_ZipManager.GetZipList(strPath,items);
151 if (items.size() > 1)