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"
29 #include "utils/StringUtils.h"
36 CZipDirectory::CZipDirectory()
40 CZipDirectory::~CZipDirectory()
44 bool CZipDirectory::GetDirectory(const CStdString& strPathOrig, CFileItemList& items)
48 /* if this isn't a proper archive path, assume it's the path to a archive file */
49 if( !StringUtils::StartsWithNoCase(strPathOrig, "zip://") )
50 URIUtils::CreateArchivePath(strPath, "zip", strPathOrig, "");
52 strPath = strPathOrig;
56 CStdString strArchive = url.GetHostName();
57 CStdString strOptions = url.GetOptions();
58 CStdString strPathInZip = url.GetFileName();
60 url.SetOptions(""); // delete options to have a clean path to add stuff too
61 url.SetFileName(""); // delete filename too as our names later will contain it
63 CStdString strSlashPath = url.Get();
67 // the RAR code depends on things having a "/" at the end of the path
68 URIUtils::AddSlashAtEnd(strSlashPath);
70 vector<SZipEntry> entries;
71 // turn on fast lookups
72 bool bWasFast(items.GetFastLookup());
73 items.SetFastLookup(true);
74 if (!g_ZipManager.GetZipList(strPath,entries))
77 vector<std::string> baseTokens;
78 if (!strPathInZip.empty())
79 StringUtils::Tokenize(strPathInZip,baseTokens,"/");
81 for (vector<SZipEntry>::iterator ze=entries.begin();ze!=entries.end();++ze)
83 CStdString strEntryName(ze->name);
84 strEntryName.Replace('\\','/');
85 if (strEntryName == strPathInZip) // skip the listed dir
88 vector<std::string> pathTokens;
89 StringUtils::Tokenize(strEntryName,pathTokens,"/");
90 if (pathTokens.size() < baseTokens.size()+1)
95 for ( unsigned int i=0;i<baseTokens.size();++i )
97 if (pathTokens[i] != baseTokens[i])
102 strEntryName += pathTokens[i] + "/";
107 strEntryName += pathTokens[baseTokens.size()];
108 char c=ze->name[strEntryName.size()];
109 if (c == '/' || c == '\\')
111 bool bIsFolder = false;
112 if (strEntryName[strEntryName.size()-1] != '/') // this is a file
114 strBuffer = strSlashPath + strEntryName + strOptions;
117 { // this is new folder. add if not already added
119 strBuffer = strSlashPath + strEntryName + strOptions;
120 if (items.Contains(strBuffer)) // already added
124 CFileItemPtr pFileItem(new CFileItem);
126 if (g_charsetConverter.isValidUtf8(pathTokens[baseTokens.size()]))
127 g_charsetConverter.utf8ToStringCharset(pathTokens[baseTokens.size()]);
129 pFileItem->SetLabel(pathTokens[baseTokens.size()]);
132 pFileItem->m_dwSize = 0;
133 URIUtils::AddSlashAtEnd(strBuffer);
136 pFileItem->m_dwSize = ze->usize;
137 pFileItem->SetPath(strBuffer);
138 pFileItem->m_bIsFolder = bIsFolder;
139 pFileItem->m_idepth = ze->method;
140 items.Add(pFileItem);
142 items.SetFastLookup(bWasFast);
146 bool CZipDirectory::ContainsFiles(const CStdString& strPath)
148 vector<SZipEntry> items;
149 g_ZipManager.GetZipList(strPath,items);
152 if (items.size() > 1)